将数据导出到Excel2007格式。

增加数据格式

        public static void TableToExcel2(DataTable table, string filename, string sheetname)
        {

                XSSFWorkbook workbook = new XSSFWorkbook();
                IFont font = workbook.CreateFont();
                font.FontName = "微软雅黑";
                font.FontHeightInPoints = 9;

                IFont fonthader = workbook.CreateFont();
                fonthader.FontName = "微软雅黑";
                fonthader.FontHeightInPoints = 9;
                fonthader.Boldweight = (short)FontBoldWeight.Bold;

                ICellStyle style1 = workbook.CreateCellStyle();
                style1.SetFont(font);

                ICellStyle stylehead = workbook.CreateCellStyle();
                stylehead.SetFont(fonthader);

                var sheet = workbook.CreateSheet(sheetname);

                var headerRow = sheet.CreateRow(0);

                //head
                foreach (DataColumn column in table.Columns)
                {

                    var cellhead = headerRow.CreateCell(column.Ordinal);//
                    cellhead.CellStyle = stylehead;
                    cellhead.SetCellValue(column.Caption);

                }

                //设置大类限制
                ArrayList cat1 = new ArrayList();
                DataSet ds = DBHelper.Instance.ExeDataSet(" select  catname from purchase_categories where iscat=1 and parentid=0 ");
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    cat1.Add(dr["catname"]);
                }

                IDataValidationHelper dvHelper = sheet.GetDataValidationHelper();
                NPOI.SS.Util.CellRangeAddressList regCat1 = new NPOI.SS.Util.CellRangeAddressList(1, 8000, 0, 0);
                IDataValidationConstraint constraint = dvHelper.CreateFormulaListConstraint("CHOICES");
                constraint.ExplicitListValues = (string[])cat1.ToArray(typeof(string));
                IDataValidation validation = dvHelper.CreateValidation(constraint, regCat1);
                sheet.AddValidationData(validation);

                //小类
                regCat1 = new NPOI.SS.Util.CellRangeAddressList(1, 8000, 1, 1);
                cat1 = new ArrayList();
                ds = DBHelper.Instance.ExeDataSet(" select  catname from purchase_categories where iscat=1 and parentid!=0 ");
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    cat1.Add(dr["catname"]);
                }

                constraint = dvHelper.CreateFormulaListConstraint("CHOICES");
                constraint.ExplicitListValues = (string[])cat1.ToArray(typeof(string));
                validation = dvHelper.CreateValidation(constraint, regCat1);
                sheet.AddValidationData(validation);

                ////单位
                regCat1 = new NPOI.SS.Util.CellRangeAddressList(1, 8000, 6, 6);
                cat1 = new ArrayList();
                DataView dv = Purchase.BLL.PurchaseHelper.GetParam("unit");
                foreach (DataRowView dvr in dv)
                {
                    cat1.Add(dvr["paramname"]);
                }
                constraint = dvHelper.CreateFormulaListConstraint("CHOICES");
                constraint.ExplicitListValues = (string[])cat1.ToArray(typeof(string));
                validation = dvHelper.CreateValidation(constraint, regCat1);
                sheet.AddValidationData(validation);

                ////货币单位
                regCat1 = new NPOI.SS.Util.CellRangeAddressList(1, 65535, 8, 8);
                cat1 = new ArrayList();
                dv = Purchase.BLL.PurchaseHelper.GetParam("currency");
                foreach (DataRowView dvr in dv)
                {
                    cat1.Add(dvr["paramname"]);
                }
                constraint = dvHelper.CreateFormulaListConstraint("CHOICES");
                constraint.ExplicitListValues = (string[])cat1.ToArray(typeof(string));
                validation = dvHelper.CreateValidation(constraint, regCat1);
                sheet.AddValidationData(validation);

                //////货币单位
                regCat1 = new NPOI.SS.Util.CellRangeAddressList(1, 65535, 11, 11);
                cat1 = new ArrayList();
                dv = Purchase.BLL.PurchaseHelper.GetParam("shipmethod");
                foreach (DataRowView dvr in dv)
                {
                    cat1.Add(dvr["paramname"]);
                }
                constraint = dvHelper.CreateFormulaListConstraint("CHOICES");
                constraint.ExplicitListValues = (string[])cat1.ToArray(typeof(string));
                validation = dvHelper.CreateValidation(constraint, regCat1);
                sheet.AddValidationData(validation);

                //body
                var rowIndex = 1;
                foreach (DataRow row in table.Rows)
                {
                    var dataRow = sheet.CreateRow(rowIndex);
                    dataRow.HeightInPoints = 20;
                    foreach (DataColumn column in table.Columns)
                    {

                        ICell cell = dataRow.CreateCell(column.Ordinal);
                        cell.SetCellValue(row[column].ToString());

                        cell.CellStyle = style1;
                    }
                    rowIndex++;
                }

            FileStream sw = File.Create(HttpContext.Current.Server.MapPath(filename));
            workbook.Write(sw);
            sw.Close();

        }

时间: 2024-11-17 06:28:25

将数据导出到Excel2007格式。的相关文章

使用NPOI将数据导出为word格式里的table

开发环境:VS2013+MySQL5.5+EF6+NPOI2.0.6 格式:WinForm+CodeFirst PS:vs2013的CodeFirst很方便了啊 CodeFirst方式就不再赘述了. 此Demo托管地址:http://git.oschina.net/uustudy/ExportImportWord.git 另外推荐下NPOI代码托管地址:https://github.com/tonyqus/npoi 作者博客:http://tonyqus.sinaapp.com/ 使用nuget

利用PHPExcel将数据导出到xls格式的excel文件

在开发某地的经营许可证管理系统的时候需要将数据导出打excel文件,虽然一年前做某集团的ERP的时候用到过一次导入和导出,但是那时候太忙没时间写博客,一年过去了我也忘的差不多了,所以趁着今天将此次的使用写到博客中,作为以后开发的一个参考,免得再次使用的时候需要导出找资料,也给其他同行一个参考.作者:DragonDean,博客地址:http://www.cnblogs.com/dragondean/ 什么是PHPExcel? PHPExcel 是用来操作Office Excel 文档的一个PHP类

【MySQL】数据导出成CSV格式

一.自动输出中文字符集 select * from db into outfile 'test.csv' CHARACTER SET gbk fields terminated by ',' optionally enclosed by '"' escaped by '"' lines terminated by '\r\n'; 二.导出后转换字符集 转换成CSV文件,如果乱码将CSV已记事本打开另存为UTF8 select * from db into outfile 'test.c

jexcel+struts2实现上传下载,,即模型数据导出为Excel格式和上传Excel格式的文件

3.将数据查询出来,自己先定义好表头的列数及表头名,将将每个对象的属性转换为一个String[],然后将所有转换成的String[]存入到List<String[]>中,并通过jexcel对表头String[]和list<String[]>进行循环遍历,将每个子字符串转换成Excel中的一个单元格,并写出到输出流中,最后再将该输出流转换为输入流返回到download视图中,最后在访问页面时就会以下载的方法呈现.download视图由struts.xml中配置各项参数.,该downl

Python脚本---把MySQL数据库表中的数据导出生成csv格式文件

转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/45841221 #!/usr/bin/env python # -*- coding:utf-8 -*- """  Purpose: 生成日汇总对账文件  Created: 2015/4/27  Modified:2015/5/1  @author: guoyJoe""" #导入模块import MySQLdbimport timeimpo

asp.net 查询到的数据导出为excel格式

DataTable pDataTable = newBLL.PRODUCT().GetModel(1862); inttRowCount = pDataTable.Rows.Count; inttColumnCount = pDataTable.Columns.Count; Response.Expires = 0; Response.Clear(); Response.Buffer = true; Response.Charset = "utf-8"; Response.Conten

数据导出到csv

3.mysql -h jconnjkgiw4vh.mysql.rds.aliyuncs.com -u jusr27zkky8u -pl9cMqv0Rjdi base_ccms_ygaor -sNe "select date(created),order_num,response_num,(response_num/order_num) as lv,send_num,response_amount,(response_amount/(send_num*0.05)) from tb_tc_urpay

支持将数据导出到Excel文档的时候设置单元格格式的.NET控件Spire.DataExport

Spire.DataExport for .NET是e-iceblue公司推出的一款数据导出类.NET控件.作为一款专业的数据导出控件,Spire.DataExport for .NET可以帮助开发人员轻松快速的从各种主流数据库中导出数据并存储于各种文件格式中.他支持从SQL Command, DataTable,ListView中导出数据并存储于MS Excel,MS Word, HTML, XML, PDF, MS Access, DBF, SQL Script, SYLK, DIF, CS

tablib把数据导出为Excel、JSON、CSV等格式的Py库(写入数据并导出exl)

#tablib把数据导出为Excel.JSON.CSV等格式的Py库 #python 3 import tablib #定义列标题 headers = ('1列', '2列', '3列', '4列', '5列') #需写入的数据,按照一行一行的输入 #元组数据的个数必须和列数一致 data = [('23','23','34','23','34'),('sadf','23','sdf','23','fsad')] #写入数据 mylist = tablib.Dataset(*data, head