生成EXCEL文件是经常需要用到的功能,我们利用一些开源库可以很容易实现这个功能。
方法一:利用excellibrary,http://code.google.com/p/excellibrary/
excellibrary是国人写的开源组件,很容易使用,可惜貌似还不支持.xlsx(Excel 2007),例子如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
//Create the data set and tableDataSet ds = new DataSet("New_DataSet");DataTable dt = new DataTable("New_DataTable");//Set the locale for eachds.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;dt.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;//Open a DB connection (in this example with OleDB)OleDbConnection con = new OleDbConnection(dbConnectionString);con.Open();//Create a query and fill the data table with the data from the DBstring sql = "SELECT Whatever FROM MyDBTable;";OleDbCommand cmd = new OleDbCommand(sql, con);OleDbDataAdapter adptr = new OleDbDataAdapter();adptr.SelectCommand = cmd;adptr.Fill(dt);con.Close();//Add the table to the data setds.Tables.Add(dt);//Here's the easy part. Create the Excel worksheet from the data setExcelLibrary.DataSetHelper.CreateWorkbook("MyExcelFile.xls", ds); |
例子二:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
//create new xls filestring file = "C:\\newdoc.xls";Workbook workbook = new Workbook();Worksheet worksheet = new Worksheet("First Sheet");worksheet.Cells[0, 1] = new Cell((short)1);worksheet.Cells[2, 0] = new Cell(9999999);worksheet.Cells[3, 3] = new Cell((decimal)3.45);worksheet.Cells[2, 2] = new Cell("Text string");worksheet.Cells[2, 4] = new Cell("Second string");worksheet.Cells[4, 0] = new Cell(32764.5, "#,##0.00");worksheet.Cells[5, 1] = new Cell(DateTime.Now, @"YYYY\-MM\-DD");worksheet.Cells.ColumnWidth[0, 1] = 3000;workbook.Worksheets.Add(worksheet);workbook.Save(file);// open xls fileWorkbook book = Workbook.Load(file);Worksheet sheet = book.Worksheets[0];// traverse cellsforeach (Pair<Pair<int, int>, Cell> cell in sheet.Cells){dgvCells[cell.Left.Right, cell.Left.Left].Value = cell.Right.Value;}// traverse rows by Indexfor (int rowIndex = sheet.Cells.FirstRowIndex; rowIndex <= sheet.Cells.LastRowIndex; rowIndex++){Row row = sheet.Cells.GetRow(rowIndex);for (int colIndex = row.FirstColIndex; colIndex <= row.LastColIndex; colIndex++){Cell cell = row.GetCell(colIndex);}} |
方法二:利用EPPlus,http://epplus.codeplex.com/
EPPlus是一个使用Open Office XML(xlsx)文件格式,能读写Excel 2007/2010 文件的开源组件。
例子如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
var file = @"Sample.xlsx";if (File.Exists(file)) File.Delete(file);using (var excel = new ExcelPackage(new FileInfo(file))){var ws = excel.Workbook.Worksheets.Add("Sheet1");ws.Cells[1, 1].Value = "Date";ws.Cells[1, 2].Value = "Price";ws.Cells[1, 3].Value = "Volume";var random = new Random();for (int i = 0; i < 10; i++){ws.Cells[i + 2, 1].Value = DateTime.Today.AddDays(i);ws.Cells[i + 2, 2].Value = random.NextDouble() * 1e3;ws.Cells[i + 2, 3].Value = random.Next() / 1e3;}ws.Cells[2, 1, 11, 1].Style.Numberformat.Format = "dd/MM/yyyy";ws.Cells[2, 2, 11, 2].Style.Numberformat.Format = "#,##0.000000"; ws.Cells[2, 3, 11, 3].Style.Numberformat.Format = "#,##0"; ws.Column(1).AutoFit();ws.Column(2).AutoFit();ws.Column(3).AutoFit();excel.Save();} |
例子二:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
using OfficeOpenXml;//指定Templete文档Text.xlsxFileInfo newFile = new FileInfo("D:" + @"\Test.xlsx");//开启using (ExcelPackage pck = new ExcelPackage(newFile)){try{//设定ExcelWorkBookExcelWorkbook workBook = pck.Workbook;if (workBook != null){if (workBook.Worksheets.Count > 0){//复制Temp这个Sheet同时命名为《清单》ExcelWorksheet currentWorksheet = workBook.Worksheets.Copy("Temp", "清单");//可以设定保护Sheet的密码//currentWorksheet.Protection.SetPassword("1234");int StartRow = 4;for (int i = 0; i < tDS.Tables[0].Rows.Count; i++){//Cells[RowIndex,CellIndex]currentWorksheet.Cells[StartRow + i, 1].Value = Convert.ToString(tDS.Tables[0].Rows[i][0]);currentWorksheet.Cells[StartRow + i, 2].Value = Convert.ToString(tDS.Tables[0].Rows[i][1]);currentWorksheet.Cells[StartRow + i, 3].Value = Convert.ToString(tDS.Tables[0].Rows[i][2]);currentWorksheet.Cells[StartRow + i, 4].Value = Convert.ToString(tDS.Tables[0].Rows[i][3]);currentWorksheet.Cells[StartRow + i, 5].Value = Convert.ToString(tDS.Tables[0].Rows[i][4]);currentWorksheet.Cells[StartRow + i, 6].Value = Convert.ToString(tDS.Tables[0].Rows[i][5]);currentWorksheet.Cells[StartRow + i, 7].Value = Convert.ToString(tDS.Tables[0].Rows[i][6]);currentWorksheet.Cells[StartRow + i, 8].Value = Convert.ToString(tDS.Tables[0].Rows[i][7]);currentWorksheet.Cells[StartRow + i, 9].Value = Convert.ToString(tDS.Tables[0].Rows[i][8]);currentWorksheet.Cells[StartRow + i, 10].Value = Convert.ToString(tDS.Tables[0].Rows[i][9]);currentWorksheet.Cells[StartRow + i, 11].Value = Convert.ToString(tDS.Tables[0].Rows[i][10]);currentWorksheet.Cells[StartRow + i, 12].Value = Convert.ToString(tDS.Tables[0].Rows[i][11]);currentWorksheet.Cells[StartRow + i, 13].Value = Convert.ToString(tDS.Tables[0].Rows[i][12]);}//将Temp 这个Sheet删除workBook.Worksheets.Delete("Temp");}}//存至Text4.xlsxpck.SaveAs(new FileInfo("H:" + @"\Test4.xlsx"));}catch (Exception e){oLogger.Fatal(e.ToString());}} |
方法三:NPOI http://npoi.codeplex.com/
NPOI无需Office COM组件且不依赖Office,使用NPOI能够帮助开发者在没有安装微软Office的情况下读写Office 97-2003的文件,支持的文件格式包括xls, doc, ppt等。NPOI是构建在POI 3.x版本之上的,它可以在没有安装Office的情况下对Word/Excel文档进行读写操作。
被人称为操作EXCEL的终极方案,例子如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
//引用using NPOI.HSSF.UserModel;using NPOI.HPSF;using NPOI.POIFS.FileSystem;using NPOI.SS.UserModel;//将WorkBook指到我们原本设计好的Templete Book1.xlsusing (IWorkbook wb = new HSSFWorkbook(new FileStream("D:/Book1.xls", FileMode.Open))){try{//设定要使用的Sheet为第0个SheetISheet TempSheet = wb.GetSheetAt(0);int StartRow = 4;//tDS为Query回来的资料for (int i = 0; i < tDS.Tables[0].Rows.Count; i++){//第一个Row要用Create的TempSheet.CreateRow(StartRow + i).CreateCell(0).SetCellValue(Convert.ToString(tDS.Tables[0].Rows[i][0]));//第二个Row之后直接用Get的TempSheet.GetRow(StartRow + i).CreateCell(1).SetCellValue(Convert.ToString(tDS.Tables[0].Rows[i][1]));TempSheet.GetRow(StartRow + i).CreateCell(2).SetCellValue(Convert.ToString(tDS.Tables[0].Rows[i][2]));TempSheet.GetRow(StartRow + i).CreateCell(3).SetCellValue(Convert.ToString(tDS.Tables[0].Rows[i][3]));TempSheet.GetRow(StartRow + i).CreateCell(4).SetCellValue(Convert.ToString(tDS.Tables[0].Rows[i][4]));TempSheet.GetRow(StartRow + i).CreateCell(5).SetCellValue(Convert.ToString(tDS.Tables[0].Rows[i][5]));TempSheet.GetRow(StartRow + i).CreateCell(6).SetCellValue(Convert.ToString(tDS.Tables[0].Rows[i][6]));TempSheet.GetRow(StartRow + i).CreateCell(7).SetCellValue(Convert.ToString(tDS.Tables[0].Rows[i][7]));TempSheet.GetRow(StartRow + i).CreateCell(8).SetCellValue(Convert.ToString(tDS.Tables[0].Rows[i][8]));TempSheet.GetRow(StartRow + i).CreateCell(9).SetCellValue(Convert.ToString(tDS.Tables[0].Rows[i][9]));TempSheet.GetRow(StartRow + i).CreateCell(10).SetCellValue(Convert.ToString(tDS.Tables[0].Rows[i][10]));TempSheet.GetRow(StartRow + i).CreateCell(11).SetCellValue(Convert.ToString(tDS.Tables[0].Rows[i][11]));TempSheet.GetRow(StartRow + i).CreateCell(12).SetCellValue(Convert.ToString(tDS.Tables[0].Rows[i][12]));}//将文档写到指定位置using (FileStream file = new FileStream("H:/Test_NPOI4.xls", FileMode.Create)){wb.Write(file);file.Close();file.Dispose();}}catch (Exception e){string a = e.ToString();}} |
生成EXCEL文件是经常需要用到的功能,我们利用一些开源库可以很容易实现这个功能。的更多相关文章
- 如何生成excel文件作为图像识别结果
如何生成excel文件作为图像识别结果 在进行大规模图像处理的时候,如果能够以表格的形式生成结果文件,将非常的直观.这个时候,选择excel作为结果输出文件,将是合适的. 查询相关资料,有很多关于ex ...
- php生成excel文件的简单方法
生成excel文件,最简单的莫过于把数据库的数据导入到excel就行了. 生成excel 当然使用的是 phpExcel http://www.jbxue.com/tags/phpexcel.html ...
- XLSTransformer生成excel文件简单演示样例
项目结构图: 项目中所用到的jar,能够到http://www.findjar.com/index.x下载 ExcelUtil类源代码: package util; import java.io.IO ...
- XLSTransformer生成excel文件
jxls的使用方法: 1)声明一个XLSTransformer对象,生成方式就是使用new操作符 XLSTransformer transformer = new XL ...
- thinkphp整合系列之phpexcel生成生成excel文件
在后台管理中会经常需要将数据生成excel表格的: php生成excel有两种方案: 一种是通过phpexcel生成xls格式的表格文件: 另一种则直接通过逗号换行生成csv格式的表格文件: 这里先讲 ...
- 实现excel导入导出功能,excel导入数据到页面中,页面数据导出生成excel文件
今天接到项目中的一个功能,要实现excel的导入,导出功能.这个看起来思路比较清楚,但是做起了就遇到了不少问题. 不过核心的问题,大家也不会遇到了.每个项目前台页面,以及数据填充方式都不一样,不过大多 ...
- springMVC(4)---生成excel文件并导出
springMVC(4)---生成excel文件并导出 在开发过程中,需要将数据库中的数据以excel表格的方式导出. 首先说明.我这里用的是Apache的POI项目,它是目前比较成熟的HSSF接口, ...
- 2018年,请不要再使用OLE生成EXCEL文件
输出EXCEL文件是ABAP开发工作中的常见需求,为了学习相关技术,我翻译过一篇文章:使用OLE2对象创建EXCEL文件,并且一度乐在其中. 最近几个月,经过与若干EXCEL打印程序的艰苦斗争,以及对 ...
- POI生成EXCEL文件
POI生成EXCEL文件 一.背景 根据指定格式的JSON文件生成对应的excel文件,需求如下 支持多sheet 支持单元格合并 支持插入图片 支持单元格样式可定制 需要 标题(title),表头( ...
随机推荐
- Android 5种Toast特效
Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失. 1.默认效果: Toast.ma ...
- Spring Data JPA(一)简介
Spring Data JPA介绍 可以理解为JPA规范的再次封装抽象,底层还是使用了Hibernate的JPA技术实现,引用JPQL(Java Persistence Query Language) ...
- ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'ambari'
配置Ambari远程maridb 报错: ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'ambari' ...
- CentOS7修改计算机名!
https://www.cnblogs.com/acgpiano/p/4170546.html sudo hostnamectl set-hostname <host-name>
- 每日踩坑 2019-07-30 H5 使用 iframe 底部有白边
用个iframe累死累活的 用 js 动态计算高度, 结果明明px都对,然后却把页面滚动条也整出来了. 查看元素盒模型也一切正常. 然后仔细观察就发现是下边多了几个像素的白色边. 然后就 百度呗 以下 ...
- 微服务SpringCloud系列
https://my.oschina.net/hmilyylimh?tab=newest&catalogId=5703366
- wxs 及 获取节点 和 网络请求
wxs:微信小程序脚本语言,结合 wxml 可以构建页面的结构 在 wxml 中使用 wxs wxs 中的注释 : 单行注释:// 多行注释:/* */ wxs 在页面中的用法 在页面中引用 wxs ...
- fw: 专访许鹏:谈C程序员修养及大型项目源码阅读与学习
C家最近也有一篇关于如何阅读大型c项目源代码的文章,学习..融合.. -------------------- ref:http://www.csdn.net/article/2014-06-05 ...
- leetcode-easy-array-48. Rotate Image-NO
mycode 思路:第m行要变到 - 1- m 列 ,但是没有再想一步即列变为行,这样每一个位置的变换方式就出来了 难点:如何不使用额外空间呢? 参考: 思路:找到矩阵旋转和转置之间的联系,转置是可以 ...
- 阶段3 1.Mybatis_01.Mybatis课程介绍及环境搭建_01.mybatis课程介绍