需求:
實現錯誤信息生成Excel保存到本地讓用戶查看。
剛開始使用了微軟自帶的Microsoft.Office.Interop.Excel類庫、
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); excel.Visible = true; //新增加一個工作簿,Workbook是直接保存,不會彈出保存對話框,加上Application會彈出保存對話框,值為false會報錯 excel.Application.Workbooks.Add(true); //生成Excel中列頭名稱 excel.Cells[1, 1] = "姓名"; excel.Cells[1, 2] = "工號"; excel.Cells[1, 3] = "公司"; excel.Cells[1, 4] = "部門"; excel.Cells[1, 5] = "失敗原因"; //把newdt當前頁的數據保存在Excel中 for (int i = 0; i < newdt.Rows.Count; i++) { excel.Cells[i + 2, 1] = "'" + newdt.Rows[i]["Emp_Name"]; excel.Cells[i + 2, 2] = "'" + newdt.Rows[i]["Emp_Code"]; excel.Cells[i + 2, 3] = "'" + newdt.Rows[i]["CompanyName"]; excel.Cells[i + 2, 4] = "'" + newdt.Rows[i]["DeptName"]; excel.Cells[i + 2, 5] = "'" + newdt.Rows[i]["Error"]; } //設置禁止彈出保存和覆蓋的詢問提示框 excel.DisplayAlerts = false; excel.AlertBeforeOverwriting = false; //保存工作簿 excel.Application.Workbooks.Add(true).Save(); //保存excel文件 excel.Save("D:" + "\\錯誤信息.xls"); //確保Excel進程關閉 excel.Quit(); excel = null;
雖然功能實現了、但是由於服務器上沒有安裝Excel、無法調用COM+組件。
並且COM+組件 聽說如果導出過程中出問題可能導致服務器宕機。
經過查閱,修改為調用Aspose.Cells組件。
/// <summary> /// 導出數據到本地 /// </summary> /// <param name="dt">要導出的數據</param> /// <param name="Name">標題</param> /// <param name="path">保存路徑</param> public void OutFileToDisk(DataTable dt, string Name, string path) { Workbook workbook = new Workbook(); /* 工作簿 */ Worksheet sheet = workbook.Worksheets[0]; /* 工作表 */ Cells cells = sheet.Cells; /* 單元格 //為標題設置樣式 */ Style styleTitle = workbook.Styles[workbook.Styles.Add()]; /* 新增樣式 */ Style style2 = workbook.Styles[workbook.Styles.Add()]; /* 新增樣式 */ style2.HorizontalAlignment = TextAlignmentType.Center; /* 文字居中 */ style2.Font.Name = "宋體"; /* 文字字體 */ style2.Font.Size = 12; /* 文字大小 */ style2.Font.IsBold = true; /* 粗體 */ style2.IsTextWrapped = true; /* 單元格內容自動換行 */ style2.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; style2.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; style2.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; style2.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; /* 樣式3 */ Style style3 = workbook.Styles[workbook.Styles.Add()]; /* 新增樣式 */ style3.HorizontalAlignment = TextAlignmentType.Center; /* 文字居中 */ style3.Font.Name = "宋體"; /* 文字字體 */ style3.Font.Size = 10; /* 文字大小 */ style3.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; style3.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; style3.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; style3.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; int Colnum = dt.Columns.Count; /* 表格列數 */ int Rownum = dt.Rows.Count; /* 生成行2 列名行 */ for (int i = 0; i < Colnum; i++) { cells[0, i].PutValue(dt.Columns[i].ColumnName); cells[0, i].SetStyle(style2); cells.SetRowHeight(0, 20); cells.SetColumnWidth(i, 20); } /* 生成數據行 */ for (int i = 0; i < Rownum; i++) { for (int k = 0; k < Colnum; k++) { cells[1 + i, k].PutValue(dt.Rows[i][k].ToString()); cells[1 + i, k].SetStyle(style3); } cells.SetRowHeight(1 + i, 18); } //workbook.Save(path);//可以通過Save直接存儲 MemoryStream ms = workbook.SaveToStream(); byte[] bt = ms.ToArray(); string fileName = Name + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xls";//客戶端保存的文件名 //以字符流的形式下載文件 Response.ContentType = "application/vnd.ms-excel"; //通知瀏覽器下載文件而不是打開 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); Response.BinaryWrite(bt); Response.Flush(); Response.End(); }
分享供大家學習、