1:使用epplus合併多個excel文件到同一excel的不同sheet頁中 2:設置excel文件sheet頁的 頁邊距(使用epplus) ...
1:使用epplus合併多個excel文件到同一excel的不同sheet頁中
private static bool MergeExcel(string _stFilePath, List<string> _listFiles, string _stSaveFileName) { ExcelPackage epMergeFile = new ExcelPackage(); bool result = true; try { string stSheetName = string.Empty; foreach (string item in _listFiles) { if (!FileIsHave(_stFilePath + "\\" + item)) continue; FileInfo newFile = new FileInfo(_stFilePath + "\\" + item); //開啟 using (ExcelPackage pck = new ExcelPackage(newFile)) { //設定ExcelWorkBook ExcelWorkbook workBook = pck.Workbook; if (workBook != null) { if (workBook.Worksheets.Count > 0) { //設置excel表格的頁邊距 //workBook.Worksheets[1].PrinterSettings.TopMargin = 10M / 2.54M; stSheetName = item.Substring(0, item.LastIndexOf('.')); epMergeFile.Workbook.Worksheets.Add(stSheetName, workBook.Worksheets[1]); } } } } } catch (Exception ex) { result = false; Debug.WriteLine("合併文件失敗:" + ex.Message); throw new Exception("合併文件失敗!"); } if (result) { string stFile = _stFilePath + "\\" + _stSaveFileName; epMergeFile.SaveAs(new FileInfo(stFile)); epMergeFile.Dispose(); } return result; }
2:設置excel文件sheet頁的 頁邊距(使用epplus)