惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Forbes - Security
Forbes - Security
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
SegmentFault 最新的问题
Y
Y Combinator Blog
Recorded Future
Recorded Future
博客园 - Franky
I
InfoQ
T
The Blog of Author Tim Ferriss
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
Cyberwarzone
Cyberwarzone
The Register - Security
The Register - Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
雷峰网
雷峰网
P
Palo Alto Networks Blog
G
GRAHAM CLULEY
Cloudbric
Cloudbric
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MongoDB | Blog
MongoDB | Blog
F
Full Disclosure
Google DeepMind News
Google DeepMind News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
Check Point Blog
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
T
Threat Research - Cisco Blogs
U
Unit 42
N
Netflix TechBlog - Medium
The Cloudflare Blog
Spread Privacy
Spread Privacy
Microsoft Azure Blog
Microsoft Azure Blog
美团技术团队
T
Troy Hunt's Blog
Engineering at Meta
Engineering at Meta
H
Heimdal Security Blog
TaoSecurity Blog
TaoSecurity Blog
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tenable Blog
B
Blog
S
Securelist
H
Hacker News: Front Page
Google Online Security Blog
Google Online Security Blog
G
Google Developers Blog

博客园 - 小寒

Postman or TestNG for automated testing adb.exe 已停止工作 解决 用户中心 - 博客园 移动UI设计 c3p0数据源的使用初步及Mysql8小时问题解决 npm+node+cordova+ionic 版本匹配 ngrok - 小寒 vi command speech recognition resource mysql Workbench 执行删除命令 备份Mysql数据库BAT脚本 C# double 四舍五入 mysql CREATE USER ConvertHelper 通用类 自定义属性 Cordova Ionic AngularJS centos mariadb Nginx 负载均衡
使用Aspose.Cells生成Excel的方法详解(转)
小寒 · 2014-08-11 · via 博客园 - 小寒

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;
         }
 
 }