使用Aspose.Cells組件怎么導出excel文件?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
具體內容如下
/// <summary>
/// 導出數據到本地
/// </summary>
/// <param name="dt">要導出的數據</param>
/// <param name="tableName">導出名稱</param>
/// <param name="tableTitle">表格行名格式“賬號,密碼”</param>
/// <param name="response">請求</param>
public static void OutFileToDisk(DataTable dt, string tableName, string tableTitle, HttpResponse response)
{
Workbook workbook = new Workbook(); //工作簿
Worksheet sheet = workbook.Worksheets[0]; //工作表
Cells cells = sheet.Cells;//單元格
//為標題設置樣式
Style styleTitle = workbook.Styles[workbook.Styles.Add()];//新增樣式
styleTitle.HorizontalAlignment = TextAlignmentType.Center;//文字居中
styleTitle.Font.Name = "宋體";//文字字體
styleTitle.Font.Size = 18;//文字大小
styleTitle.Font.IsBold = true;//粗體
//樣式2
Style style2 = workbook.Styles[workbook.Styles.Add()];//新增樣式
style2.HorizontalAlignment = TextAlignmentType.Center;//文字居中
style2.Font.Name = "宋體";//文字字體
style2.Font.Size = 14;//文字大小
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 = 12;//文字大小
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;//表格行數
//生成行1 標題行
cells.Merge(0, 0, 1, Colnum);//合并單元格
cells[0, 0].PutValue(tableName);//填寫內容
cells[0, 0].SetStyle(styleTitle);
cells.SetRowHeight(0, 38);
//生成行2 列名行
string[] Tile = tableTitle.Split(',');
for (int i = 0; i < Colnum; i++)
{
cells[1, i].PutValue(Tile[i]);
cells[1, i].SetStyle(style2);
cells.SetRowHeight(1, 25);
}
//生成數據行
for (int i = 0; i < Rownum; i++)
{
for (int k = 0; k < Colnum; k++)
{
cells[2 + i, k].PutValue(dt.Rows[i][k].ToString());
cells[2 + i, k].SetStyle(style3);
}
cells.SetRowHeight(2 + i, 24);
}
workbook.Save(response, HttpUtility.UrlEncode(tableName, System.Text.Encoding.UTF8) + ".xls", ContentDisposition.Attachment, new XlsSaveOptions(SaveFormat.Excel97To2003));
}調用
string tableTitle = "賬號,密碼"; ExcelHelp.OutFileToDisk(dt, "賬戶信息", tableTitle , HttpContext.Current.Response);
前臺頁面
window.open("方法", "_blank");//點擊下載看完上述內容,你們掌握使用Aspose.Cells組件怎么導出excel文件的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。