乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > <em>Aspose</em>.Cells如何将datatable导出Exce...-aspos

<em>Aspose</em>.Cells如何将datatable导出Exce...-aspos

作者:乔山办公网日期:

返回目录:excel表格制作


HTML 到 Excel ,最好是使用 加载 Excel 模板,然后填入数据的导出方式。
因为Excel对html标签的样式兼容性存在很大的问题。

你可以看看 aspose.Cells 这个插件的说明书。

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 = "宋体";//文字字636f7079e79fa5e98193335体 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; } } public static bool ExportExcelWithAspose(System.Data.DataTable dt, string path) { bool succeed = false; if (dt != null) { try { Aspose.Cells.License li = new Aspose.Cells.License(); string lic = Resources.License; Stream s = new MemoryStream(ASCIIEncoding.Default.GetBytes(lic)); li.SetLicense(s); Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(); Aspose.Cells.Worksheet cellSheet = workbook.Worksheets[0]; cellSheet.Name = dt.TableName; int rowIndex = 0; int colIndex = 0; int colCount = dt.Columns.Count; int rowCount = dt.Rows.Count; //列名的处理 for (int i = 0; i < colCount; i++) { cellSheet.Cells[rowIndex, colIndex].PutValue(dt.Columns[i].ColumnName); cellSheet.Cells[rowIndex, colIndex].Style.Font.IsBold = true; cellSheet.Cells[rowIndex, colIndex].Style.Font.Name = "宋体"; colIndex++; } Aspose.Cells.Style style = workbook.Styles[workbook.Styles.Add()]; style.Font.Name = "Arial"; style.Font.Size = 10; Aspose.Cells.StyleFlag styleFlag = new Aspose.Cells.StyleFlag(); cellSheet.Cells.ApplyStyle(style, styleFlag); rowIndex++; 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()); colIndex++; } rowIndex++; } cellSheet.AutoFitColumns(); path = Path.GetFullPath(path); workbook.Save(path); succeed = true; } catch (Exception ex) { succeed = false; } } return succeed; }
Aspose.Cells要将数据库绑定到一个工作表,应遵循以下步骤:
访问将被绑定的表
指定一个数据源e799bee5baa6e997aee7ad94e78988e69d83365(可以是一个DataSet,DataTable或DataView等)的工作表
创建列绑定到数据源
创建验证。在我们的实例中,我们将为ProductID列创建一个必要的验证
指定数字格式设置。在我们的实例中,我们将产品价格列的数字格式设置为Currency3
加载和填充数据集
实现绑定
实例代码:
[C#]
//Implementing Page_Load event handler
private void Page_Load(object sender, System.EventArgs e)
{
//Checking if there is not any PostBack
if (!IsPostBack)
{
//Accessing a desired worksheet
WebWorksheet sheet = GridWeb1.WebWorksheets[0];
//Specifying Data Source for the worksheet
sheet.DataSource = dataSet11;
//Specifying Products tables as the DataMember
sheet.DataMember = "Products";
//Creating data bound columns automatically
sheet.CreateAutoGenratedColumns();
//Creating REQUIRED validation for ProductID column
Validation v = new Validation();
v.IsRequired = true;
sheet.BindColumns[0].Validation = v;
//Setting Number Format of ProductPrice column to Currency3
sheet.BindColumns[2].NumberType = NumberType.Currency3;
try
{
//Filling DataSet
oleDbDataAdapter1.Fill(dataSet11);
}
finally
{
//Closing database connection
oleDbConnection1.Close();
}
//Binding worksheet with DataSet
sheet.DataBind();
}
}

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.IO; using System.Data; using Aspose.Cells; /// ///OutFileDao 的摘要说明 /// publicclass OutFileDao { public OutFileDao() { // //TODO: 在此处添加构造函数逻辑 // } /// /// 测试程序 /// 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"); } /// /// 导出数据到本地 /// /// 要导出的数据 /// 表格标题 /// 保存路径 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;//文字居中e68a84e79fa5e98193366 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; } } public static bool ExportExcelWithAspose(System.Data.DataTable dt, string path) { bool succeed = false; if (dt != null) { try { Aspose.Cells.License li = new Aspose.Cells.License(); string lic = Resources.License; Stream s = new MemoryStream(ASCIIEncoding.Default.GetBytes(lic)); li.SetLicense(s); Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(); Aspose.Cells.Worksheet cellSheet = workbook.Worksheets[0]; cellSheet.Name = dt.TableName; int rowIndex = 0; int colIndex = 0; int colCount = dt.Columns.Count; int rowCount = dt.Rows.Count; //列名的处理 for (int i = 0; i < colCount; i++) { cellSheet.Cells[rowIndex, colIndex].PutValue(dt.Columns[i].ColumnName); cellSheet.Cells[rowIndex, colIndex].Style.Font.IsBold = true; cellSheet.Cells[rowIndex, colIndex].Style.Font.Name = "宋体"; colIndex++; } Aspose.Cells.Style style = workbook.Styles[workbook.Styles.Add()]; style.Font.Name = "Arial"; style.Font.Size = 10; Aspose.Cells.StyleFlag styleFlag = new Aspose.Cells.StyleFlag(); cellSheet.Cells.ApplyStyle(style, styleFlag); rowIndex++; 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()); colIndex++; } rowIndex++; } cellSheet.AutoFitColumns(); path = Path.GetFullPath(path); workbook.Save(path); succeed = true; } catch (Exception ex) { succeed = false; } } return succeed; }

相关阅读

关键词不能为空

ppt怎么做_excel表格制作_office365_word文档_365办公网