日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

aspose将datatable导出excel 比自己拼好的多 Bug少-。.net

發布時間:2023/12/13 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 aspose将datatable导出excel 比自己拼好的多 Bug少-。.net 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? using System;?
using System.Collections.Generic;?
using System.Linq;?
using System.Web;?
using System.IO;?
using System.Data;?
using Aspose.Cells;?

/// <summary>?
///OutFileDao 的摘要說明?
/// </summary>?
publicclass OutFileDao?
{?
????????public OutFileDao()?
????????{?
????????????????//?
????????????????//TODO: 在此處添加構造函數邏輯?
????????????????//?
????????}?

????????/// <summary>?
????????/// 測試程序?
????????/// </summary>?
????????publicstaticvoid testOut()?
????????{?

????????????????DataTable dt = new DataTable();?
????????????????dt.Columns.Add("name");?
????????????????dt.Columns.Add("sex");?
????????????????DataRow dr = dt.NewRow();?
????????????????dr["name"] = "名稱1";?
????????????????dr["sex"] = "性別1";?
????????????????dt.Rows.Add(dr);?

????????????????DataRow dr1 = dt.NewRow();?
????????????????dr1["name"] = "名稱2";?
????????????????dr1["sex"] = "性別2";?
????????????????dt.Rows.Add(dr1);?

????????????????OutFileToDisk(dt, "測試標題", @"d:\測試.xls");?
????????}?

????????/// <summary>?
????????/// 導出數據到本地?
????????/// </summary>?
????????/// <param name="dt">要導出的數據</param>?
????????/// <param name="tableName">表格標題</param>?
????????/// <param name="path">保存路徑</param>?
????????publicstaticvoid OutFileToDisk(DataTable dt,string tableName,string path)?
????????{?


????????????????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 列名行?
????????????????for (int i = 0; i < Colnum; i++)?
????????????????{?
????????????????????????cells[1, i].PutValue(dt.Columns[i].ColumnName);?
????????????????????????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(path);?
????????}?


????????public MemoryStream OutFileToStream(DataTable dt, string tableName)?
????????{?
????????????????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 列名行?
????????????????for (int i = 0; i < Colnum; i++)?
????????????????{?
????????????????????????cells[1, i].PutValue(dt.Columns[i].ColumnName);?
????????????????????????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);?
????????????????}?

????????????????MemoryStream ms = workbook.SaveToStream();?
????????????????return ms;?
????????}?

}

轉載于:https://www.cnblogs.com/JQlin-c/archive/2012/04/19/2456849.html

總結

以上是生活随笔為你收集整理的aspose将datatable导出excel 比自己拼好的多 Bug少-。.net的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。