使用Aspose.Cells生成Excel的方法详解(转)

using System; using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.IO;
 using System.Data;
 using Aspose.Cells;
 
/// <summary>
///OutFileDao 的摘要说明
/// </summary>
 public class OutFileDao
 {
         public OutFileDao()
         {
                 //
                 //TODO: 在此处添加构造函数逻辑
                 //
         }
 
         /// <summary>
         /// 测试程序
         /// </summary>
         public static void 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>
         public static void 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;
         }
 
 }

使用Aspose.Cells生成Excel的方法详解(转)

时间: 2024-08-08 06:10:58

使用Aspose.Cells生成Excel的方法详解(转)的相关文章

Aspose.Cells 操作Excel相关文章

Aspose.Cells 根据Excel模板导出数据统计 使用Aspose.Cell控件实现Excel高难度报表的生成(一)

Aspose.cells 处理Excel文件

//浏览Excel文件 private void btnScan_Click(object sender, RoutedEventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.DefaultExt = "xlsx"; ofd.Filter = "Excel file|*.xlsx"; if (ofd.ShowDialog() == true) { this.txtFilePath.Text = of

四种生成和解析XML文档的方法详解(介绍+优缺点比较+示例)

四种生成和解析XML文档的方法详解(介绍+优缺点比较+示例) 众所周知,现在解析XML的方法越来越多,但主流的方法也就四种,即:DOM.SAX.JDOM和DOM4J 下面首先给出这四种方法的jar包下载地址 DOM:在现在的Java JDK里都自带了,在xml-apis.jar包里 SAX:http://sourceforge.net/projects/sax/ JDOM:http://jdom.org/downloads/index.html DOM4J:http://sourceforge.

Aspose.cells 导出Excel

//Export private void btnExport_Click(object sender, RoutedEventArgs e) { string strDataTime = System.DateTime.Now.ToString("yyyyMMdd_HHmmss"); string strFileName = "buyf_Template" + strDataTime; SaveFileDialog save = new SaveFileDialo

Aspose.Cells导出excel

利用Aspose.Cells导出excel 注意的问题 1.DataTable的处理 2.进行编码,便于中文名文件下载 3.别忘了Aspose.Cells.dll(可以自己在网上搜索) public static bool DataTableToExcel2(DataTable datatable, string filepath, out string error) { error = ""; Aspose.Cells.Workbook wb = new Aspose.Cells.W

报表中的Excel操作之Aspose.Cells(Excel模板)

本文转载:http://www.cnblogs.com/whitewolf/archive/2011/03/21/Aspose_Cells_Template1.html 报表中的Excel操作之Aspose.Cells(Excel模板)

Aspose.Cells将Excel转成pdf

Aspose.Cells Excel转成pdf /// <summary> /// excel转pdf /// </summary> /// <param name="path">文件地址</param> /// <param name="newFilePath">转换后的文件地址</param> /// <returns></returns> public static

JavaScript原生对象属性和方法详解——Array对象 转载

length 设置或返回 数组中元素的数目. 注意:设置 length 属性可改变数组的大小.如果设置的值比其当前值小,数组将被截断,其尾部的元素将丢失.如果设置的值比它的当前值大,数组将增大,新的元素被添加到数组的尾部,它们的值为 undefined.所以length不一定代表数组的元素个数. var arr = new Array(3) arr[0] = "John" arr[1] = "Andy" arr[2] = "Wendy" cons

Python数据类型及其方法详解

Python数据类型及其方法详解 我们在学习编程语言的时候,都会遇到数据类型,这种看着很基础也不显眼的东西,却是很重要,本文介绍了python的数据类型,并就每种数据类型的方法作出了详细的描述,可供知识回顾. 一.整型和长整型 整型:数据是不包含小数部分的数值型数据,比如我们所说的1.2.3.4.122,其type为"int" 长整型:也是一种数字型数据,但是一般数字很大,其type为"long" 在python2中区分整型和长整型,在32位的机器上,取值范围是-2