0.准備工作
1.下載並引入Aspose.Cells
下載Aspose Cells
並引入using Aspose.Cells 下面示例中用的是.net 3.0版本的Aspose Cells,編譯環境VS2013
具體下載和引入方法見:
http://www.cnblogs.com/moonache/p/4991459.html
1.使用Aspose將DataTable轉為Excel
1.代碼
下面代碼用於將DataTable dt 轉為Excel文件並存在path目錄下
/// <summary> /// DataTable轉Excel文件 /// </summary> /// <param name="dt"></param> /// <param name="path"></param> /// <returns></returns> public static bool ExportExcelWithAspose(DataTable dt, string path) { if (dt != null) { try { Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(); Aspose.Cells.Worksheet cellSheet = workbook.Worksheets[0]; //為head添加樣式 Aspose.Cells.Style headStyle = workbook.Styles[workbook.Styles.Add()]; //設置居中 headStyle.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center; //設置背景顏色 headStyle.ForegroundColor = System.Drawing.Color.FromArgb(215, 236, 241); headStyle.Pattern = BackgroundType.Solid; headStyle.Font.Size = 12; headStyle.Font.Name = "宋體"; headStyle.Font.IsBold = true; //為單元格添加樣式 Aspose.Cells.Style cellStyle = workbook.Styles[workbook.Styles.Add()]; //設置居中 cellStyle.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center; cellStyle.Pattern = BackgroundType.Solid; cellStyle.Font.Size = 12; cellStyle.Font.Name = "宋體"; //設置列寬 從0開始 列寬單位是字符 cellSheet.Cells.SetColumnWidth(1, 43); cellSheet.Cells.SetColumnWidth(5, 12); cellSheet.Cells.SetColumnWidth(7, 10); cellSheet.Cells.SetColumnWidth(8, 14); cellSheet.Cells.SetColumnWidth(9, 14); int rowIndex = 0; int colIndex = 0; int colCount = dt.Columns.Count; int rowCount = dt.Rows.Count; //Head 列名處理 for (int i = 0; i < colCount; i++) { cellSheet.Cells[rowIndex, colIndex].PutValue(dt.Columns[i].ColumnName); cellSheet.Cells[rowIndex, colIndex].SetStyle(headStyle); colIndex++; } rowIndex++; //Cell 其它單元格處理 for (int i = 0; i < rowCount; i++) { colIndex = 0; for (int j = 0; j < colCount; j++) { cellSheet.Cells[rowIndex, colIndex].PutValue(dt.Rows[i][j].ToString()); cellSheet.Cells[rowIndex, colIndex].SetStyle(cellStyle); colIndex++; } rowIndex++; } cellSheet.AutoFitColumns(); //列寬自動匹配,當列寬過長是收縮 path = Path.GetFullPath(path); //workbook.Save(path,SaveFormat.CSV); workbook.Save(path); return true; } catch (Exception e) { throw new Exception("導出Excel失敗" + e.Message); } } else { return false; } }
下面代碼用於直接在頁面輸出Excel文件,供用戶下載
WonderTools.ExportExcelWithAspose(templateDt,filePath); //提供excel的下載 HttpResponse _Response = HttpContext.Current.Response; _Response.Clear(); _Response.ClearHeaders(); _Response.Buffer = false; _Response.ContentType = "application/x-excel"; _Response.AppendHeader("Content-Disposition", "attachment;filename=Template.xlsx"); _Response.WriteFile(fileInfo.FullName); _Response.Flush(); _Response.End();
PPS:
直接通過 ashx 獲取數據庫中的數據並下載 Excel
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; context.Response.AddHeader("Content-Disposition", "attachment;filename=" + context.Server.UrlEncode("數據.xlsx")); using (var dt = SqlHelper.ExecuteQuery("SELECT * FROM T_Users")) { var workbook = new Workbook(); var sheet = workbook.Worksheets[0]; // Header for (var i = 0; i < dt.Columns.Count; i++) { sheet.Cells[0, i].PutValue(dt.Columns[i].ColumnName); } // Content for (var i = 0; i < dt.Rows.Count; i++) { for (var j = 0; j < dt.Columns.Count; j++) { sheet.Cells[i + 1, j].PutValue(dt.Rows[i][j].ToString()); } } workbook.Save(context.Response.OutputStream,SaveFormat.Xlsx); } }