Excel相关Aspose
 
public class ExportToExcel { private string SheetName = ""; private string outFileName = ""; private string fullFilename = ""; private Workbook book = null; private Worksheet sheet = null; /// <summary> /// 导出构造函数 /// </summary> /// <param name="sheetname">工作簿名称</param> /// <param name="page">页面对象</param> public ExportToExcel(string sheetname) { SheetName = sheetname; book = new Workbook(); sheet = book.Worksheets[0]; Aspose.Cells.License li = new License(); string path = "D:License.lic"; li.SetLicense(path); } ///// <summary> ///// 导入构造数 ///// </summary> ///// <param name="fullfilename">上传文件路径</param> //public ExportToExcel(string fullfilename) //{ // fullFilename = fullfilename; // // book = new Workbook(); // // book.Open(tempfilename); // // sheet = book.Worksheets[0]; //} private void AddTitle(DataTable dt) { //sheet.Cells.Merge(0, 0, 1, columnCount); //sheet.Cells.Merge(1, 0, 1, columnCount); //Cell cell1 = sheet.Cells[0, 0]; //cell1.PutValue(title); //cell1.Style.HorizontalAlignment = TextAlignmentType.Center; //cell1.Style.Font.Name = "黑体"; //cell1.Style.Font.Size = 14; //cell1.Style.Font.IsBold = true; //Cell cell2 = sheet.Cells[1, 0]; //cell1.PutValue("查询时间:" + DateTime.Now.ToLocalTime()); //cell2.SetStyle(cell1.Style); for (int c = 0; c < dt.Columns.Count; c++) { sheet.Cells[0, c].PutValue(dt.Columns[c].ColumnName); sheet.Cells[0, c].Style.Font.IsBold = true; sheet.Cells[0, c].Style.Font.Name = "黑体"; sheet.Cells[0, c].Style.Font.Size = 14; } } private void AddHeader(DataTable dt) { Cell cell = null; for (int col = 0; col < dt.Columns.Count; col++) { cell = sheet.Cells[0, col]; cell.PutValue(dt.Columns[col].ColumnName); cell.Style.Font.IsBold = true; } } private void AddBody(DataTable dt) { for (int r = 0; r < dt.Rows.Count; r++) { for (int c = 0; c < dt.Columns.Count; c++) { sheet.Cells[r + 1, c].PutValue(dt.Rows[r][c].ToString()); } } } /// <summary> /// 导出 /// </summary> /// <param name="dt">数据表</param> /// <returns></returns> public Boolean DatatableToExcel(DataTable dt) { Boolean yn = false; try { sheet.Name = SheetName; AddTitle(dt); //AddHeader(dt); AddBody(dt); sheet.AutoFitColumns(); sheet.AutoFitRows(); string filename = DateTime.Now.ToString("yyyyMMddHHmmss"); book.Save(filename + ".xls"); yn = true; return yn; } catch (Exception e) { return yn; } } /// <summary> /// 导出 /// </summary> /// <param name="dt">数据表</param> /// <param name="heading">标题</param> /// <param name="subheading">子标题</param> /// <returns></returns> public Boolean DatatableToExcel(DataTable dt, string heading) { try { sheet.Name = SheetName; AddTitle(dt); //AddHeader(dt); AddBody(dt); sheet.Cells.InsertRows(0, 1); sheet.Cells[0, 0].PutValue(heading); sheet.Cells[0, 0].Style.Font.IsBold = true; sheet.Cells[0, 0].Style.Font.Size = 16; sheet.Cells[0, 0].Style.HorizontalAlignment = TextAlignmentType.Center; sheet.Cells.Merge(0, 0, 1, dt.Columns.Count); //sheet.Cells[1, dt.Columns.Count / 2].PutValue(subheading); sheet.AutoFitColumns(); sheet.AutoFitRows(); string filename = DateTime.Now.ToString("yyyyMMddHHmmss"); book.Save(filename + ".xls"); return true; } catch (Exception e) { return false; } } /// <summary> /// 导出 /// </summary> /// <param name="dt">数据表</param> /// <param name="heading">标题</param> /// <param name="subheading">子标题</param> /// <returns></returns> public Boolean DatatableToExcel(DataTable dt, string heading, string subheading) { try { sheet.Name = SheetName; AddTitle(dt); //AddHeader(dt); AddBody(dt); sheet.Cells.InsertRows(0, 2); sheet.Cells[0, 0].PutValue(heading); sheet.Cells[0, 0].Style.Font.IsBold = true; sheet.Cells[0, 0].Style.Font.Size = 16; sheet.Cells[0, 0].Style.HorizontalAlignment = TextAlignmentType.Center; sheet.Cells.Merge(0, 0, 1, dt.Columns.Count); sheet.Cells[1, dt.Columns.Count / 2].PutValue(subheading); sheet.AutoFitColumns(); sheet.AutoFitRows(); string filename = DateTime.Now.ToString("yyyyMMddHHmmss"); book.Save(filename + ".xls"); return true; } catch (Exception e) { return false; } } /// <summary> /// 导入 /// </summary> /// <returns></returns> public DataTable ExcelToDataTable() { Workbook book = new Workbook(); book.Open(fullFilename); Worksheet sheet = book.Worksheets[0]; Cells cells = sheet.Cells; //获取excel中的数据保存到一个datatable中 DataTable dt_Import = cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, true); System.IO.File.Delete(fullFilename); return dt_Import; } /// <summary> /// 导入 /// </summary> /// <returns></returns> public DataTable ExcelToDataTable(int Rowindex) { Workbook book = new Workbook(); book.Open(fullFilename); Worksheet sheet = book.Worksheets[0]; Cells cells = sheet.Cells; //获取excel中的数据保存到一个datatable中 DataTable dt_Import = cells.ExportDataTableAsString(Rowindex, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, true); System.IO.File.Delete(fullFilename); return dt_Import; } }
需要注册文件和dll
 
                    
                
 
 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号