C# NPOI Excel 템플릿 내보내기

7111 단어
1. winForm Excel 템플릿 내보내기
 
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();

 
다음으로 전송:https://www.cnblogs.com/chirs888888/p/11607745.html

좋은 웹페이지 즐겨찾기