C#使用openxml合并多个excel到一个文档

 public void MergeExcelFiles(string[] as_excel_paths)
        {
            var ls_first_file = as_excel_paths.Length > 0 ? as_excel_paths[0] : "";
            var index = 0;
            if (ls_first_file.fn_isempty())
            {
                return;
            }
            using (var lo_excel = new ExcelPackage(new FileInfo(ls_first_file)))
            {
                using (var lo_sheets = lo_excel.Workbook.Worksheets)
                {
                    for (var i = 1; i < as_excel_paths.Length; i++)
                    {
                        var lo_file = new FileInfo(as_excel_paths[i]);
                        using (var lo_excel_copy = new ExcelPackage(lo_file))
                        {
                            using (var lo_copy_sheets = lo_excel_copy.Workbook.Worksheets)
                            {
                                foreach (var lo_copy_sheet in lo_copy_sheets)
                                {
                                    index++;
                                    //length - all max: 31, index: max 4
                                    var ls_merge_sheet_name = lo_file.Name.fn_left(23) + (lo_file.Name.fn_left(23).Length < lo_file.Name.Length ? "..." : "") + "_" + index;
                                    while (lo_sheets[ls_merge_sheet_name] != null)
                                    {
                                        index++;
                                        ls_merge_sheet_name = lo_file.Name.fn_left(23) + (lo_file.Name.fn_left(23).Length < lo_file.Name.Length ? "..." : "") + "_" + index;
                                    }
                                    ls_merge_sheet_name = ls_merge_sheet_name.fn_left(31);
                                    if (lo_sheets[ls_merge_sheet_name] != null)
                                    {
                                        ls_merge_sheet_name = lo_file.Name.fn_left(23) + Guid.NewGuid().ToString().Replace("-", "").fn_right(8);
                                    }
                                    lo_sheets.Add(ls_merge_sheet_name, lo_copy_sheet);
                                    lo_excel.Save();
                                }

                            }

                        }
                    }
                }
               
    
            }
            
           
        }

你可能感兴趣的:(服务器,linux,c#,excel)