/// <summary> /// 根据现有的既定行数,导出Excel文件 /// </summary> /// <param name="allRows"></param> /// <param name="excelFileCount"></param> /// <param name="exportFileInfo"></param> /// <param name="tablesCountDic"></param> /// <param name="querySqlPart"></param> /// <param name="whereSqlPart"></param> /// <param name="token"></param> /// <param name="exportProgressAction"></param> /// <param name="exportFileCompletedAction"></param> public void ExportExcel(int allRows, int excelFileCount, ExportFileInfo exportFileInfo, Dictionary <string, int> tablesCountDic, string querySqlPart, string whereSqlPart, CancellationToken token, Action <int> exportProgressAction, Action <string> exportFileCompletedAction) { List <string> tableNameList = tablesCountDic.Keys.ToList(); List <int> tableCountList = tablesCountDic.Values.ToList(); int sheetCount = allRows / Pub.DEFAULT_EXCELEXPORT_SHEET_MAXCOUNT + allRows % Pub.DEFAULT_EXCELEXPORT_SHEET_MAXCOUNT == 0 ? 0 : 1; Func <int, string> getCurrectTableName = (int tableIndex) => { return(tableNameList[tableIndex]); }; using (MySqlConnection conn = SqlHelper.GetConnection()) { conn.Open(); int tableIndex = 0; int currectTableOccupancyRows = tableCountList[tableIndex]; //当前表的剩余记录数 int currectTableStartRowIdx = 0; //当前表起始行号 int dispId = 0; for (int fileIndex = 1; fileIndex <= excelFileCount; fileIndex++) { string filePath = ExcelFileHelper.GetExportFileName(exportFileInfo.ExcelTitle, exportFileInfo.SaveDirectory, fileIndex, excelFileCount); QueryDataReaderWriteExcel(conn, allRows, sheetCount, exportFileInfo, filePath, token, querySqlPart, tableNameList, tableCountList, whereSqlPart, ref tableIndex, ref currectTableOccupancyRows, ref currectTableStartRowIdx, ref dispId, exportProgressAction); if (exportFileCompletedAction != null) { exportFileCompletedAction(filePath); } } } }
/// <summary> /// List导出到Excel /// </summary> /// <param name="allRows"></param> /// <param name="excelFileCount"></param> /// <param name="exportFileInfo"></param> /// <param name="list"></param> /// <param name="token"></param> /// <param name="exportProgressAction"></param> /// <param name="exportFileCompletedAction"></param> public void ExportListToExcel(int allRows, int excelFileCount, ExportFileInfo exportFileInfo, List <T> list, CancellationToken token, Action <int> exportProgressAction, Action <string> exportFileCompletedAction) { int sheetCount = allRows / Pub.DEFAULT_EXCELEXPORT_SHEET_MAXCOUNT + (allRows % Pub.DEFAULT_EXCELEXPORT_SHEET_MAXCOUNT == 0 ? 0 : 1); int currectListOccupancyRows = list.Count; //当前表的剩余记录数 int dispId = 0; for (int fileIndex = 1; fileIndex <= excelFileCount; fileIndex++) { string filePath = ExcelFileHelper.GetExportFileName(exportFileInfo.ExcelTitle, exportFileInfo.SaveDirectory, fileIndex, excelFileCount); ListWriteExcelFile(allRows, sheetCount, exportFileInfo, filePath, list, token, ref currectListOccupancyRows, ref dispId, exportProgressAction); if (exportFileCompletedAction != null) { exportFileCompletedAction.Invoke(filePath); } } }