ExcelにC#npoiバッチ追記データ

直接コードに添付:

使用してDongYang.Core.Model.Domainを。
使用してDongYang.Core.Utilsを。
使ってNLogを。
使用してNPOI.XSSF.UserModelを。
使用してシステムを。
使用してSystem.Collections.Genericを。
使用してSystem.Dataを。
使用したSystem.Diagnosticsを。
使用してSystem.IOを。
使用してSystem.Threadingを。

名前空間DongYang.Core.Service 
{ 
    パブリック クラスDYTrackANODetail700013TodayNewService 
    { 
        プライベート 読み取り専用ロガー_logger = LogManager.GetCurrentClassLogger(); // 日志组件

        ///  <要約>
        /// 导出エクセル
         ///  </要約> 
        ///  ます。<param name = "anomateexcels"> </ param>の
        ///  <PARAM NAME = "CURRENTTIME"> </ param>の
        公共 のボイド ExportToExcel(一覧<Anomateexcel> anomateexcels、日時CURRENTTIME)
        { 
            ストップウォッチSW = 新しいストップウォッチ()。
            sw.Start(); 

            FileStreamファイル = nullを文字列 strBeginTime = 文字列 .Empty。// 查询开始时间
            文字列 strEndTime = 文字列 .Empty。

            
            { 
                // テンプレートファイル
                文字列 TemplateFileName AppDomain.CurrentDomain.BaseDirectory + = " \\ template.xlsx " ;
                 // エクスポートファイル
                文字列 ReportFileName = FileHelper.GetExportFilePath(CURRENTTIME)+ $ " \\ {currentTime.ToString(" YYYYMMDD_HHMMSS " )} .xlsx " ;
                 //は、クエリーの日付を取得
                この .GetTime(CURRENTTIME、OUT strBeginTime、OUT strEndTimeを);
                 // データのクエリ総数
                int型の COUNT = この.GetDataTableCount(strBeginTime、strEndTime);
                 IF(COUNT == 0リターン;
                 // 最初のコピー、空のファイル
                File.Copy(TemplateFileName、ReportFileName);
                 // ループの内側のページクエリデータは、ファイルのコピーやデータの追加を開いて、クローズファイルハンドル
                VARは、ページ= Math.Ceiling(Convert.ToDouble(COUNT)/ ConfigHelper.PageSize);
                 のためint型、pageIndexパラメータ= 1 ; pageIndexパラメータ<=ページ; pageIndexパラメータ++ 
                { 
                    VAR(pageIndexパラメータ- startRow属性を= 。1)* ConfigHelper.PageSize + 1 VARとendRow pageIndexパラメータ* = ConfigHelper.PageSize;
                     VARの DT = この.GetDataTable(strBeginTime、strEndTime、startRow属性、とendRow); 

                    ファイル = 新しい新しいFileStreamを(ReportFileName、FileMode.Open、FileAccess.Read、FileShare.ReadWrite); 
                    XSSFWorkbook xssfworkbook = 新しい新しい XSSFWorkbook (ファイル); // メモリにファイル操作メモリでExcel 
                    File.close(); 
                    xSSFSheet xssfsheet = xssfworkbook.GetSheet(ConfigHelper.WorkSheetName)AS xSSFSheet; 

                    VAR beginRow =による3 + startRow属性-1 ;
                    ためVAR iが= 0 ; I <dt.Rows.Count; I ++ 
                    { 
                        VAR excelRow = xssfsheet.CreateRow(beginRow ++ )。
                        foreachのVARの anomateexcel anomateexcels)
                        { 
                            VAR excelCell = excelRow.CreateCell(anomateexcel.Excellist.ToInt())。
                            excelCell.SetCellType(NPOI.SS.UserModel.CellType.String)。
                            VaRの値= dt.Rows [I] [anomateexcel.Anofield] .ToString(); 
                            excelCell.SetCellValue(値)。
                        } 
                    } 

                    Xssfsheet.ForceFormulaRecalculation = trueに;
                     // ファイルのデータがメモリに書き込まれる
                    使用(FS = FileStreamをFile.OpenWrite(ReportFileName))
                    { 
                        xssfworkbook.Write(FS); 
                        xssfworkbook.Close(); 
                    } 

                    のThread.sleep(100 )。
                } 
            } 
            キャッチ(例外EX)
            { 
                _logger.Error($ "エクスポートデータエラー、メッセージ:ex.Message {}、スタックトレース:ex.StackTrace {} " )。 
            } 
            最後に
            { 
                IF(!ファイル= ヌル)File.close(); 
            } 

            sw.Stop(); 
            _logger.Info($ " 日:{CURRENTTIME}、{sw.Elapsed.TotalSeconds}加工秒" ); 
        } 

        #region取得開始時刻と終了時刻/// <まとめ> /// 取得開始時刻と終了時刻
         /// </要約> / // ます。<param name = "CURRENTTIME"> </ param>の/// <PARAM NAME = "strBeginTime"> </ param>の/// <PARAM NAME = "strEndTime"></ param>のプライベート

         
         
         
         
         
         ボイド GetTime(日時CURRENTTIME、ストリング strBeginTime、アウトstrEndTime)
        { 
            日時BEGINTIME。
            日時endTimeは、もし(CURRENTTIME <currentTime.Date.AddHours(7.5 ))
            { 
                BEGINTIME = currentTime.Date.AddDays( - 1).AddHours(7.5)。// 前天午前7時30分 
                たendTime = currentTime.Date.AddHours(7.5)。// 当天的午前7時30             }
             
            { 
                BEGINTIMEアウト  
            
 = currentTime.Date.AddHours(7.5 )。
                endTimeは = currentTime.Date.AddDays(1).AddHours(7.5 )。
            } 

            strBeginTime = beginTime.ToString(" YYYY-MM-DD HH:MM " )。
            strEndTime = endTime.ToString(" YYYY-MM-DD HH:MM " )。
        } 

        #endregion 

        #region查询数据/// <要約> /// 查询数据
         /// </要約> /// <PARAM NAME = "strBeginTime"> </ PARAM> /// <PARAM NAME = "strEndTime" > </ param>の/// <

         
         
         
         
         
         ///  <PARAM NAME = "とendRow"> </ PARAM> 
        ///  <戻ります> </リターン> 
        民間のDataTable GetDataTable(文字列 strBeginTime、文字列 strEndTime、int型 startRow属性、int型とendRow)
        { 
            VARのsql = $ @」
SELECT * FROM 
    、ROWNUMBERとしてASC、ABのASC、ACのASCによって(オーダー)の上に選択ROW_NUMBER()* 
    {ConfigHelper.ExcelExportTableName}から
    ここでE '{strBeginTime}'と'{strEndTime}'との間の
)Tと
ここで、{} startRow属性間ROWNUMBERと{}とendRow
" ; 
            データテーブルDT  =DapperSqlHelper.QueryDataTable(SQL);
             戻りDT; 
        } 

        #endregion 

        #region查询总数/// <要約> /// 查询总数
         /// </要約> /// <PARAM NAME =" strBeginTime "> </ param>の/// <PARAM NAME = "strEndTime"> </ param>の/// <リターン> </リターン> プライベートint型 GetDataTableCount(文字列 strBeginTime、文字列strEndTime)
        { VARのsql = $ "

         
         
         
         
         
         
             SELECT COUNT(1) '{strBeginTime}'の間のE {} ConfigHelper.ExcelExportTableNameからそして、 '
            データテーブルDT = DapperSqlHelper.QueryDataTable(SQL)。
            VaRのカウント= Convert.ToInt32(dt.Rows [ 0 ] [ 0 ])。
            戻り値の数。
        } 

        #endregion 
    } 
}

データはおそらく、レコードごとに7または8000、および400フィールド大であるので、条件に沿ってデータのワンタイム読書として始まりました。その後、そのように最適化されたページの読み込み、各撮影データ100になった場合、データはエクセル挿入するように下がります。

おすすめ

転載: www.cnblogs.com/subendong/p/12109568.html