• 大小: 1.61MB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2023-08-16
  • 语言: C#
  • 标签: Winform  NPOI  

资源简介

将Winform中的datagridview中的数据通过Excel导出,用SaveFileDialog指定位置导出,用到的技术是NPOI,不受office组件的影响。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NPOI;
using NPOI.HPSF;
using NPOI.HSSF;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.POIFS;
using NPOI.Util;
using System.IO;
using System.Data;

namespace SaveFileDialogDemo
{
    class ExportXls
    {
        /// 
        /// 由DataTable导出Excel
        /// 

        /// 要导出数据的DataTable
        /// Excel工作表
        public void ExportDataTableToExcel(DataTable sourceTable string sheetName string filepath)
        {

            FileStream file = new FileStream(filepath FileMode.Create);
            HSSFWorkbook workbook = new HSSFWorkbook();
            // MemoryStream ms = new MemoryStream();
            ISheet sheet = workbook.CreateSheet(sheetName);
            IRow headerRow = sheet.CreateRow(0);
            // handling header.
            foreach (DataColumn column in sourceTable.Columns)

                headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
            // handling value.
            int rowIndex = 1;
            foreach (DataRow row in sourceTable.Rows)
            {
                IRow dataRow = sheet.CreateRow(rowIndex);
                foreach (DataColumn column in sourceTable.Columns)
                {
                    dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
                }
                rowIndex++;
            }
            workbook.Write(file);
            file.Close();
            sheet = null;
            headerRow = null;
            workbook = null;
        }

    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件     200704  2013-01-02 20:15  SaveFileDialogDemo\bin\Debug\ICSharpCode.SharpZipLib.dll

     文件    1600000  2014-01-05 10:51  SaveFileDialogDemo\bin\Debug\NPOI.dll

     文件     396288  2014-01-05 10:51  SaveFileDialogDemo\bin\Debug\NPOI.OOxml.dll

     文件      84480  2014-01-05 10:51  SaveFileDialogDemo\bin\Debug\NPOI.Openxml4Net.dll

     文件    1867776  2014-01-05 10:51  SaveFileDialogDemo\bin\Debug\NPOI.OpenxmlFormats.dll

     文件      12800  2014-10-15 11:23  SaveFileDialogDemo\bin\Debug\SaveFileDialogDemo.exe

     文件      30208  2014-10-15 11:23  SaveFileDialogDemo\bin\Debug\SaveFileDialogDemo.pdb

     文件      11600  2014-10-15 11:23  SaveFileDialogDemo\bin\Debug\SaveFileDialogDemo.vshost.exe

     文件        490  2010-03-17 22:39  SaveFileDialogDemo\bin\Debug\SaveFileDialogDemo.vshost.exe.manifest

     文件       1746  2014-10-15 11:22  SaveFileDialogDemo\ExportXls.cs

     文件    1600000  2014-01-05 10:51  SaveFileDialogDemo\NPIO\NPOI.dll

     文件       4440  2014-10-15 11:15  SaveFileDialogDemo\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       7277  2014-10-15 11:23  SaveFileDialogDemo\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件       1223  2014-10-10 13:32  SaveFileDialogDemo\obj\x86\Debug\edmxResourcesToembed\Model1.csdl

     文件       1080  2014-10-10 13:32  SaveFileDialogDemo\obj\x86\Debug\edmxResourcesToembed\Model1.msl

     文件       1121  2014-10-10 13:32  SaveFileDialogDemo\obj\x86\Debug\edmxResourcesToembed\Model1.ssdl

     文件        414  2014-10-10 15:05  SaveFileDialogDemo\obj\x86\Debug\GenerateResource.read.1.tlog

     文件        970  2014-10-10 15:05  SaveFileDialogDemo\obj\x86\Debug\GenerateResource.write.1.tlog

     文件      32404  2014-10-15 11:15  SaveFileDialogDemo\obj\x86\Debug\ResolveAssemblyReference.cache

     文件       1127  2014-10-15 11:23  SaveFileDialogDemo\obj\x86\Debug\SaveFileDialogDemo.csproj.FileListAbsolute.txt

     文件      12800  2014-10-15 11:23  SaveFileDialogDemo\obj\x86\Debug\SaveFileDialogDemo.exe

     文件      30208  2014-10-15 11:23  SaveFileDialogDemo\obj\x86\Debug\SaveFileDialogDemo.pdb

     文件        180  2014-10-10 13:33  SaveFileDialogDemo\obj\x86\Debug\SaveFileDialogDemo.Properties.Resources.resources

     文件        180  2014-10-10 15:05  SaveFileDialogDemo\obj\x86\Debug\SaveFileDialogDemo.SaveFileDialogDemo.resources

     文件      46080  2014-10-10 13:25  SaveFileDialogDemo\obj\x86\Debug\TempPE\DYAlarmDataSet.Designer.cs.dll

     文件       7680  2014-10-10 13:32  SaveFileDialogDemo\obj\x86\Debug\TempPE\Model1.Designer.cs.dll

     文件        523  2014-10-10 10:51  SaveFileDialogDemo\Program.cs

     文件       1472  2014-10-10 10:51  SaveFileDialogDemo\Properties\AssemblyInfo.cs

     文件       2865  2014-10-10 10:51  SaveFileDialogDemo\Properties\Resources.Designer.cs

     文件       5612  2014-10-10 10:51  SaveFileDialogDemo\Properties\Resources.resx

............此处省略22个文件信息

评论

共有 条评论