NPOI copy of the template as a new excel

Use NPOI components

 

 

excel class

Quote

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.XSSF.UserModel;
using System.IO;

#region 正式导出无bug
    public HSSFWorkbook DataTableToExcelOK(DataTable dt, DataTable dt1, DataTable dt2, string filePath, string tpath, bool isColumnName)
    {
       
        IRow row = null;
        ISheet sheet = null;
        ICell cell = null;
        int startRow = 0;
        IWorkbook workbook = null;
        FileInfo ff = new FileInfo(tpath);
        FileStream fs2 = null;

        if (ff.Exists)
        {
            ff.Delete();
        }
        FileStream fs = new FileStream(tpath, FileMode.Create, FileAccess.ReadWrite);
        HSSFWorkbook x1 = new HSSFWorkbook();
        x1.Write(fs);
        fs.Close();

        FileStream fileRead = new FileStream(filePath, FileMode.Open, FileAccess.Read);//打开模板
        HSSFWorkbook hssfworkbook = new HSSFWorkbook(fileRead);

        FileStream fileSave2 = newThe FileStream (tpath, FileMode.Open, FileAccess.Read); // open excel newly created 
        HSSFWorkbook Book2 = new new HSSFWorkbook (fileSave2); 

        HSSFSheet CPS = hssfworkbook.GetSheetAt ( 6 ) AS HSSFSheet; // Gets sheet template 
        
        CPS.CopyTo (Book2, " quote system V1.5 " , to true , to true ); // copy the template to the new excel in 

        a using (the FileStream FileSave = new new the FileStream (tpath, FileMode.Open, FileAccess.Write)) 
        { 
            book2.Write ( FileSave); 
            fileSave.Close ();

            #region import data to excel in
             the using (FS2 = File.OpenRead (the tpath)) 
            { 
                // 2007 version   
                IF (tpath.IndexOf ( " .xlsx " )> 0 ) 
                    Workbook = new new XSSFWorkbook (FS2);
                 // 2003 version   
                else  IF (tpath.IndexOf ( " * .xls " )> 0 ) 
                    Workbook = new new HSSFWorkbook (FS2); 


                IF (! Workbook = null ) 
                {
                    sheet = workbook.GetSheetAt(0);
                    if (sheet != null)
                    {
                        int rowCount = sheet.LastRowNum;//总行数 
                        if (rowCount > 0)
                        {
                            IRow firstRow = sheet.GetRow(3);//表头行  
                            int cellCount = firstRow.LastCellNum;//表头列数  
                            if (isColumnName)
                            {
                                startRow = 3;// If the first row is the column name, read from the second line 
                                IF (dt! = Null && dt.Rows.Count> 0 ) 
                                { 

                                    sheet.GetRow ( . 1 ) .GetCell ( . 1 ) .SetCellValue (DT1. the Rows [ 0 ] [ 0 ] .ToString ()); // DT1 header data needs to be filled 
                                    sheet.GetRow ( . 1 ) .GetCell ( . 4 ) .SetCellValue (dt1.Rows [ 0 ] [ 0 ] .ToString ()) ; 
                                    sheet.GetRow ( . 1 ) .GetCell ( . 7 ) .SetCellValue (dt1.Rows [ 0 ] [1].ToString());

                                    int rowtbCount = Convert.ToInt32(dt.Rows.Count + 3);//datatable行数 dt内容数据 
                                    int columnCount = dt.Columns.Count;//列数  

                                    for (int i = startRow; i < rowtbCount; i++)
                                    {
                                        // row = sheet.CreateRow(i+1);
                                        row = sheet.CopyRow(i, i + 1);
                                        for (int j = 0; j < columnCount; j++)
                                        {
                                            cell Row.GetCell = (J + 2 ); // second line start writing data excel   
                                            IF (J == . 7 ) 
                                            { 
                                                cell.SetCellValue (dt.Rows [I - . 3 ] [J] .ToDouble ()); / / dt.Rows [I -. 3] [J] .ToDouble () 
                                            }
                                             the else 
                                            { 
                                                cell.SetCellValue (dt.Rows [I - . 3 ][j].ToString());
                                            } 
                                        }

                                    }

                                    sheet.GetRow(rowtbCount + 2).GetCell(9).SetCellValue(dt2.Rows[0][3].ToString());
                                    sheet.GetRow(rowtbCount + 2).GetCell(16).SetCellValue(dt2.Rows[0][0].ToString());
                                    sheet.GetRow(rowtbCount + 2).GetCell(18).SetCellValue(dt2.Rows[0][1].ToString());
                                    sheet.GetRow (rowtbCount+ 2 ) .GetCell ( 22 is ) .SetCellValue (dt2.Rows [ 0 ] [ 2 ] .ToString ()); // DT2 footer data 

                                    the using (FS2 = File.OpenWrite (the tpath)) 
                                    { 
                                        workbook.Write (FS2); // write data to the open file xls   
                                    } 
                                } 
                            } 
                        } 
                    } 
                } 
            } 
            #endregion 

            return Workbook AS HSSFWorkbook; 
        } 
    }
    #endregion
View Code

 

Controller

Quote

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.XSSF.UserModel;

. 1  #region exported in the correct template
 2      public FileResult ImportTemplateExcel ( String BOM, String ISItem)
 . 3      {
 . 4  
. 5          IWorkbook Book = new new HSSFWorkbook ();
 . 6          HSSFWorkbook the fact that newBook = new new HSSFWorkbook ();
 . 7          NFine.Code.Excel.NPOIExcel2 E = new new NPOIExcel2 ();
 . 8  
. 9          the DataTable DT0 = costquoteapp.ImportExcelBom (BOM, ISItem); // content 
10          the DataTable DT1 = costquoteapp.ImportExcelBomHeader (BOM, null ,null , to Convert.ToDateTime ( " 2018-05-05 " )); // header 
. 11          the DataTable costquoteapp.ImportExcelSum DT2 = (BOM, ISItem); // end of the table 
12 is  
13 is          String fileName = " operational costs template table V1.0 .xls " ; // client saved file name 
14          String filePath = Server.MapPath ( " ~ / operating costs table V1.0.xls " ); // template path 
15  
16          IF ! (Directory.Exists (Server.MapPath ( " ~ / TempFiles " )))
 . 17          {
 18 is             Directory.CreateDirectory (the Server.MapPath ( " ~ / TempFiles " ));
 . 19          }
 20 is          // save the generated file to a temporary folder in the server
 21 is          //   filepath = Path.Combine (the Server.MapPath ( "~ / TempFiles" ), fileName);
 22 is          //   the fact that newBook = e.SheetCopy (fileName, filePath, filePath); 
23 is          String Files = the Server.MapPath ( " ~ / TempFiles / internal operating cost table * .xls " ); // Excel the new position 
24  
25          Book = e.DataTableToExcelOK (DT0, DT1, DT2, filePath, Files, to true );
 26 is          // written to the client 
27         System.IO.MemoryStream ms = new System.IO.MemoryStream();
28         book.Write(ms);
29         ms.Seek(0, System.IO.SeekOrigin.Begin);
30         return File(ms, "application/vnd.ms-excel", filePath);
31     }
32     #endregion
View Code

Reception call the method Jiuhaola

 

XSSFWorkbook 2007 version

HSSFWorkbook 2003 version

 

Guess you like

Origin www.cnblogs.com/Julyra/p/11506118.html