Aspose.Cells是一款功能強大的 Excel 文檔處理和轉換控件,不依賴 Microsoft Excel 環境,支持全部 Excel 格式類型的操做。html
下載 Aspose.Cells.dll數據庫
Workbook workbook = new Workbook("E:\\test.xlsx"); Cells cells = workbook.Worksheets[0].Cells; for (int i = 0; i < cells.MaxDataRow + 1; i++) { for (int j = 0; j < cells.MaxDataColumn + 1; j++) { string s = cells[i, j].StringValue.Trim(); //一行行的讀取數據,插入數據庫的代碼也能夠在這裏寫 } }
Workbook workbook = new Workbook("E:\\test.xlsx"); Cells cells = workbook.Worksheets[0].Cells; System.Data.DataTable dataTable1 = cells.ExportDataTable(1, 0, cells.MaxDataRow, cells.MaxColumn);//沒有標題 System.Data.DataTable dataTable2 = cells.ExportDataTable(0, 0, cells.MaxDataRow + 1, cells.MaxColumn, true);//有標題
無標題瀏覽器
有標題服務器
生成Excelapp
/// <summary> /// DataTable數據導出Excel /// </summary> /// <param name="data"></param> /// <param name="filepath"></param> public static void DataTableExport(DataTable data, string filepath) { try { //Workbook book = new Workbook("E:\\test.xlsx"); //打開工做簿 Workbook book = new Workbook(); //建立工做簿 Worksheet sheet = book.Worksheets[0]; //建立工做表 Cells cells = sheet.Cells; //單元格 //建立樣式 Aspose.Cells.Style style = book.Styles[book.Styles.Add()]; style.Borders[Aspose.Cells.BorderType.LeftBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //應用邊界線 左邊界線 style.Borders[Aspose.Cells.BorderType.RightBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //應用邊界線 右邊界線 style.Borders[Aspose.Cells.BorderType.TopBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //應用邊界線 上邊界線 style.Borders[Aspose.Cells.BorderType.BottomBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //應用邊界線 下邊界線 style.HorizontalAlignment = TextAlignmentType.Center; //單元格內容的水平對齊方式文字居中 style.Font.Name = "宋體"; //字體 style1.Font.IsBold = true; //設置粗體 style.Font.Size = 11; //設置字體大小 //style.ForegroundColor = System.Drawing.Color.FromArgb(153, 204, 0); //背景色 //style.Pattern = Aspose.Cells.BackgroundType.Solid; //背景樣式 //style.IsTextWrapped = true; //單元格內容自動換行 //表格填充數據 int Colnum = data.Columns.Count;//表格列數 int Rownum = data.Rows.Count;//表格行數 //生成行 列名行 for (int i = 0; i < Colnum; i++) { cells[0, i].PutValue(data.Columns[i].ColumnName); //添加表頭 cells[0, i].SetStyle(style); //添加樣式 //cells.SetColumnWidth(i, data.Columns[i].ColumnName.Length * 2 + 1.5); //自定義列寬 //cells.SetRowHeight(0, 30); //自定義高 } //生成數據行 for (int i = 0; i < Rownum; i++) { for (int k = 0; k < Colnum; k++) { cells[1 + i, k].PutValue(data.Rows[i][k].ToString()); //添加數據 cells[1 + i, k].SetStyle(style); //添加樣式 } cells[1 + i, 5].Formula = "=B" + (2 + i) + "+C" + (2 + i);//給單元格設置計算公式,計算班級總人數 } sheet.AutoFitColumns(); //自適應寬 book.Save(filepath); //保存 GC.Collect(); } catch (Exception e) { logger.Error("生成excel出錯:" + e.Message); } }
調用Excel(例子)dom
public void DownExcel() { //建立DataTable DataTable dt = new DataTable("Table_AX"); dt.Columns.Add("班級名稱", System.Type.GetType("System.String")); dt.Columns.Add("男生人數", System.Type.GetType("System.String")); dt.Columns.Add("女生人數", System.Type.GetType("System.String")); dt.Columns.Add("今日請假", System.Type.GetType("System.String")); dt.Columns.Add("今日遲到", System.Type.GetType("System.String")); Random ran = new Random(); for (int i = 1; i < 6; i++) { DataRow dr = dt.NewRow(); dr["班級名稱"] = "軟件技術" + i + "班"; dr["男生人數"] = ran.Next(1, 50); dr["女生人數"] = ran.Next(1, 50); dr["今日請假"] = ran.Next(0, 10); dr["今日遲到"] = ran.Next(1, 30); dt.Rows.Add(dr); } //地址 string fileName = "班級概況.xls"; string filePath = AppDomain.CurrentDomain.BaseDirectory.Replace("\\", "/") + "GxContent/data/student/" + fileName + ""; //生成Excel ExcelHelper.DataTableExport(dt, filePath); //下載 if (System.IO.File.Exists(filePath)) { try { System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath); if (fileInfo.Exists == true) { //每次讀取文件,只讀取1M,這樣能夠緩解服務器的壓力 const long ChunkSize = 1048576; byte[] buffer = new byte[ChunkSize]; Response.Clear(); //獲取文件 System.IO.FileStream iStream = System.IO.File.OpenRead(filePath); //獲取下載的文件總大小 long dataLengthToRead = iStream.Length; Response.ContentType = "application/octet-stream"; //通知瀏覽器下載文件而不是打開 Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName); using (iStream)//解決文件佔用問題,using 外 iStream.Dispose() 沒法釋放文件 { while (dataLengthToRead > 0 && Response.IsClientConnected) { int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//讀取的大小 Response.OutputStream.Write(buffer, 0, lengthRead); Response.Flush(); dataLengthToRead = dataLengthToRead - lengthRead; } iStream.Dispose(); iStream.Close(); } Response.End(); } } catch { Response.Write("<script>alert('文件未佔用或文件未生成,請稍後重試!');window.close();</script>"); } } else { Response.Write("<script>alert('文件還未生成完,請稍後重試!');window.close();</script>"); } }
效果post