npoi导出excel(模板)

摘要:
文件Exists){MessageBox.Show(“导出所需的动态链接库NPOI.dll不存在,并且不支持export.File.Exists){Message Box.Show(”DataUsageReportingFileSample.xls模板文件不存在。请确认此文件包含在与.EXE相同的路径中。
/// <summary>
        /// 应用开源NPOI,导出Excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNPOIExport_Click(object sender, EventArgs e)
        {
            if (!File.Exists(sExePath + "NPOI.dll"))
            {
                MessageBox.Show("导出所需动态链接库NPOI.dll不存在,不支持导出。", "提示");
                return;
            }
            if (!File.Exists(sExePath + sExcelName))
            {
                MessageBox.Show("DataUsageReportingFileSample.xls模板文件不存在,请确认与.EXE同路径下包含此文件。", "提示");
                return;
            }

            // 填充数据
            using (SaveFileDialog saveExcel = new SaveFileDialog())
            {
                saveExcel.Filter = "Excel文件 (*.xls)|*.xls";
                string sNewFileName = string.Empty;
                if (saveExcel.ShowDialog() == DialogResult.OK)
                {
                    sNewFileName = saveExcel.FileName;
                    // 文件已被打开,则提示关闭
                    if (CFileHasOpened.FileHasOpen(sNewFileName))
                    {
                        MessageBox.Show("文件已被打开,请关闭后再重试保存。", "提示");
                        return;
                    }
                    // 复制模板,以后的操作都在复制的文件上进行
                    File.Copy(sExePath + sExcelName, sNewFileName, true);

                    InitializeWorkbook(sNewFileName);
                    if (null == hssfworkbook)
                    { return; }
                    ISheet modelSheet = hssfworkbook.GetSheet("Market Data Usage");
                    // 单元格格式
                    ICellStyle CenterStyle = hssfworkbook.CreateCellStyle();
                    CenterStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;

                    if (null == modelSheet)
                    { return; }
                    if (null == QueriedPermissions)
                    { return; }

                    for (Int32 rowIndex = 0; rowIndex < QueriedPermissions.Count; rowIndex++)
                    {
                        IRow tmpRow = modelSheet.GetRow(rowIndex + 5);
                        if (null == tmpRow)
                        { continue; }
                        for (Int32 colIndex = 0; colIndex < QueriedPermissions[rowIndex].Count; colIndex++)
                        {
                            ICell tmpCell = tmpRow.GetCell(colIndex);
                            if (null == tmpCell)
                            { continue; }
                            if (colIndex < 7)
                            {
                                tmpCell.SetCellValue(QueriedPermissions[rowIndex][colIndex].ToString().Trim());
                            }
                            else
                            {
                                tmpCell.SetCellValue(Convert.ToInt32(QueriedPermissions[rowIndex][colIndex].ToString().Trim()));
                                //tmpCell.CellStyle = CenterStyle;
                            }
                        }
                    }

                    //Force <a href="http://www.it165.net/edu/ebg/" target="_blank" class="keylink">excel</a> to recalculate all the formula while open
                    modelSheet.ForceFormulaRecalculation = true;

                    WriteToExcelWithNPOI(sNewFileName);
                }
            }
        }

        private static HSSFWorkbook hssfworkbook;
        /// <summary>
        /// 初始化工作簿
        /// </summary>
        private void InitializeWorkbook(string sNewFileName)
        {
            FileStream file = new FileStream(sNewFileName, FileMode.Open, FileAccess.Read);
            if (null == file)
            { return; }
            hssfworkbook = new HSSFWorkbook(file);
            if (null == hssfworkbook)
            { return; }
            //create a entry of DocumentSummaryInformation
            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "test";
            hssfworkbook.DocumentSummaryInformation = dsi;

            //create a entry of SummaryInformation
            SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
            si.Subject = "test";
            hssfworkbook.SummaryInformation = si;
        }

        /// <summary>
        /// 把工作簿写到本地文件
        /// </summary>
        private void WriteToExcelWithNPOI(string sNewFileName)
        {
            FileStream file = new FileStream(sNewFileName, FileMode.Create);
            hssfworkbook.Write(file);
            file.Close();
        }

免责声明:文章转载自《npoi导出excel(模板)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇KL散度jquery中的事件对象下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

NPOI的使用

NPOI:  对Excel表数据的导入导出(当然也有word之类,不过一般用途为Excel) 准备:  下载NPOI相关文件,http://npoi.codeplex.com  解压,添加项目对:NPOI.dll、NPOI.OOXML.dll、NPOI.OpenXml4Net.dll 的引用,这个涉及到office版本的支持问题,      office2...

C#中NPOI操作excel之读取和写入excel数据

一、下载引用 下载需要引用的dll,即:NPOI.dll,NPOI.OOXML.dll,NPOI.OpenXml4Net.dll,ICSharpCode.SharpZipLib.dll(office2007版需要此dll)。 二、excel转datatable类 [csharp]view plaincopy usingSystem; usingSy...

NPOI 添加下拉列表

需求 给指定列添加下拉列表。如下图: 思路 NPOI的文档网站不能访问了,这里参考的POI文档。 加下拉列表有两种方式,一种直接写字符串,例如 new String[]{"10", "20", "30"})。这种方式限制最大长度255. 第二种引用其他单元格。本例中采取的这种方式。新建一个Sheet,将所有选项填入其中。如图中第一行是客户信息,第二行...

.net excel利用NPOI导入oracle

1.链接数据库   引用System.Data.OracleClient;   //数据库链接字符串   Data Source如:192.168.5.153:1521/orcl   string linkStr = "User ID=" + name + "; Password=" + password + "; Data Source=" + oraL...

NPOI导出EXCEL数据量大,分多个sheet显示数据

//NPOIHelper  类关键代码 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.IO;using NPOI.HSSF.UserModel;using System.Coll...

Npoi XWPF Word 导出时插入图片无法显示

npoi中XWPFRun.AddPicture,各种尝试,各种看源代码,也无法将插入的图片显示出来,用RAR程序打开word查看Document.xml文件,提示xml文件错误.在网上找到java的poi的解决办法,自定义Pic元素. int EMU = 9525; width *= EMU; he...