C# NPOI 导出Excel(模板,xp系统 .net 3.5,.net4.5)

1. winForm 导出 Excel模板。.net 3.5

 

SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Filter = "Excel(97-2003)|*.xls|Excel(2007)|*.xlsx";
            if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
            {
                return;
            }
            string sql = "Select* from YSFGGA where GGA001 = 'FT19003955'";
            DataTable dt1 = Common.SQLHelper.ExecuteDataTable(CommandType.Text, sql, null);
            sql = "Select* from YSFGGB where GGB001 = 'FT19003955'";
            DataTable dt2 = Common.SQLHelper.ExecuteDataTable(CommandType.Text, sql, null);
            //-----------------------------------------------------------------------------
            string TempletFileName = @"template\CHUANJIN_INV_TMP.xlsx";      //模板文件  

            FileStream file = null;
            
            try
            {
                file = new FileStream(TempletFileName, FileMode.Open, FileAccess.Read);
            }
            catch (Exception ex)
            {
                
                return;
            }
            /// NuGet 管理器下载NPOI
            /// HSSFWorkbook .xls
            //HSSFWorkbook workbook = new HSSFWorkbook(file);
            //ISheet sheet = workbook.CreateSheet("I");
            //IRow rowHead = sheet.CreateRow(0);

            /// XSSFWorkbook excel 2007 .xls 
            XSSFWorkbook xssfworkbook = new XSSFWorkbook(file);
            ISheet sheet = xssfworkbook.GetSheetAt(0);//.CreateSheet();
            //IRow rowHead = sheet.CreateRow(0);
            //填写表头
            //for (int i = 0; i < dt2.Columns.Count; i++)
            //{
            //    break;
            //    rowHead.CreateCell(i, CellType.String).SetCellValue("");
            //}
            //填写内容
            for (int i = 0; i < dt2.Rows.Count; i++)
            {
                //IRow row = sheet.CreateRow(i + 17);
                IRow row = sheet.GetRow(i + 17);
                row.GetCell(1).SetCellValue("这里存放物品编码");// GetCell() 保存模板样式
                row.GetCell(2).SetCellValue("这里存放物品名称");
                row.GetCell(5).SetCellValue(1000);

                //for (int j = 0; j < dt2.Columns.Count; j++)
                //{
                //    row.CreateCell(j, CellType.String).SetCellValue(dataGridView1.Rows[i].Cells[j].Value.ToString());
                //}
            }
            sheet.ForceFormulaRecalculation = true;
            
            using (FileStream stream = File.OpenWrite(fileDialog.FileName))
            {
                xssfworkbook.Write(stream);
                stream.Close();
            }
            MessageBox.Show("导出数据成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            GC.Collect();

 

2. xp系统,.NET Framework 3.5 NPOI 2.3.0

//xp 导出EXCEL:NuGet 管理器下载NPOI 2.3.0
            if (dtResult == null) { MessageBox.Show("没有记录"); return; }
            if (dtResult.Rows.Count == 0) { MessageBox.Show("没有记录"); return; }
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.FileName = "查询";
            fileDialog.Filter = "Excel(97-2003)|*.xls";
            //fileDialog.Filter = "Excel(97-2003)|*.xls|Excel(2007)|*.xlsx";
            if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
            {
                return;
            }
            /// HSSFWorkbook .xls
            HSSFWorkbook workbook = new HSSFWorkbook();
            ISheet sheet = workbook.CreateSheet("I");
            IRow rowHead = sheet.CreateRow(0);
            sheet.SetColumnWidth(0, 10 * 256);
            sheet.SetColumnWidth(1, 15 * 256);
            sheet.SetColumnWidth(2, 16 * 256);
            sheet.SetColumnWidth(3, 10 * 256);
            sheet.SetColumnWidth(4, 10 * 256);/// XSSFWorkbook excel 2007 .xls 
            //XSSFWorkbook xssfworkbook = new XSSFWorkbook(file);
            //ISheet sheet = xssfworkbook.GetSheetAt(0);//.CreateSheet();
            //IRow rowHead = sheet.CreateRow(0);
            //填写表头
            ICellStyle cellStyle = workbook.CreateCellStyle();
            cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
            cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
            cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
            cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
            rowHead.CreateCell(0, CellType.String).SetCellValue("人账号");
            rowHead.CreateCell(1, CellType.String).SetCellValue("");
            rowHead.CreateCell(2, CellType.String).SetCellValue("单据号");
            rowHead.CreateCell(3, CellType.String).SetCellValue("编号");
            rowHead.GetCell(0).CellStyle = cellStyle;
            rowHead.GetCell(1).CellStyle = cellStyle;
            rowHead.GetCell(2).CellStyle = cellStyle;
            rowHead.GetCell(3).CellStyle = cellStyle;
            rowHead.GetCell(4).CellStyle = cellStyle;
            rowHead.GetCell(5).CellStyle = cellStyle;//填写内容
            for (int i = 0; i < dtResult.Rows.Count; i++)
            {
                IRow row = sheet.CreateRow(i + 1);
                for (int j = 0; j < dtResult.Columns.Count; j++)
                {
                    row.CreateCell(j, CellType.String).SetCellValue(dtResult.Rows[i][j].ToString().Trim());
                    row.Cells[j].CellStyle = cellStyle;
                }
            }
            sheet.ForceFormulaRecalculation = true;
            try
            {
                using (FileStream stream = File.OpenWrite(fileDialog.FileName))
                {
                    workbook.Write(stream);
                    stream.Close();
                }
                try
                {
                    System.Diagnostics.Process.Start(fileDialog.FileName);
                    GC.Collect();
                }
                catch (Exception ex)
                {
                    GC.Collect();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                GC.Collect();
            }

 

3. .net framework 4.5  NPOI 2.5.6

if (dtResult == null) { MessageBox.Show("没有记录"); return; }
            if (dtResult.Rows.Count == 0) { MessageBox.Show("没有记录"); return; }
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.FileName = "查询";
            fileDialog.Filter = "Excel(97-2003)|*.xls";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                /// NuGet 管理器下载NPOI 2.5.6
                /// HSSFWorkbook .xls
                HSSFWorkbook workbook = new HSSFWorkbook();
                ISheet sheet = workbook.CreateSheet("查询");
                IRow rowHead = sheet.CreateRow(0);
                sheet.SetColumnWidth(0, 10 * 256);
                sheet.SetColumnWidth(1, 15 * 256);
                sheet.SetColumnWidth(2, 16 * 256);
                sheet.SetColumnWidth(3, 10 * 256);
                sheet.SetColumnWidth(4, 10 * 256);
                ICellStyle cellStyle = workbook.CreateCellStyle();
                cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
                cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
                cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
                cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
                ///填写表头
                rowHead.CreateCell(0, CellType.String).SetCellValue("账号");
                rowHead.CreateCell(2, CellType.String).SetCellValue("单据号");
                rowHead.CreateCell(3, CellType.String).SetCellValue("编号");
                rowHead.CreateCell(4, CellType.String).SetCellValue("卡号");
                rowHead.CreateCell(12, CellType.String).SetCellValue("状态");
                rowHead.CreateCell(13, CellType.String).SetCellValue("回收备注");
                rowHead.GetCell(0).CellStyle = cellStyle;
                rowHead.GetCell(1).CellStyle = cellStyle;
                rowHead.GetCell(2).CellStyle = cellStyle;
                rowHead.GetCell(3).CellStyle = cellStyle;
                rowHead.GetCell(4).CellStyle = cellStyle;
                rowHead.GetCell(5).CellStyle = cellStyle;//填写内容
                for (int i = 0; i < dtResult.Rows.Count; i++)
                {
                    IRow row = sheet.CreateRow(i + 1);
                    for (int j = 0; j < dtResult.Columns.Count; j++)
                    {
                        row.CreateCell(j, CellType.String).SetCellValue(dtResult.Rows[i][j].ToString().Trim());
                        row.GetCell(j).CellStyle = cellStyle;
                        //row.CreateCell(j, CellType.String).SetCellValue(dtResult.Rows[i][j].ToString().Trim());
                    }
                }
                sheet.ForceFormulaRecalculation = true;
                try
                {
                    using (FileStream stream = File.OpenWrite(fileDialog.FileName))
                    {
                        workbook.Write(stream);
                        stream.Close();
                    }
                    try
                    {
                        System.Diagnostics.Process.Start(fileDialog.FileName);
                        GC.Collect();
                    }
                    catch (Exception ex)
                    {
                        GC.Collect();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    GC.Collect();
                }

            }

 

posted @ 2019-09-29 14:37  丁晨  阅读(1509)  评论(0编辑  收藏  举报