|
| 1 | +using System.Data; |
| 2 | +using Syncfusion.XlsIO; |
| 3 | +using Syncfusion.XlsIORenderer; |
| 4 | +using Syncfusion.Pdf; |
| 5 | + |
| 6 | +static class SyncfusionXlsIO |
| 7 | +{ |
| 8 | + public static void Run(string method, string sourcePath, DataTable table, string outputStem) |
| 9 | + { |
| 10 | + switch (method) |
| 11 | + { |
| 12 | + case "OpenAndSave": OpenAndSave(sourcePath, outputStem); break; |
| 13 | + case "CreateAndSave": CreateAndSave(table, outputStem); break; |
| 14 | + case "ImportDataTable": ImportDataTable(table, outputStem); break; |
| 15 | + case "ExportDataTable": ExportDataTable(sourcePath, table.Rows.Count); break; |
| 16 | + case "ExcelToCsv": ExcelToCsv(sourcePath, outputStem); break; |
| 17 | + case "ExcelToJson": ExcelToJson(sourcePath, outputStem); break; |
| 18 | + case "ExcelToPdf": ExcelToPdf(sourcePath, outputStem); break; |
| 19 | + default: throw new ArgumentException($"Unknown method '{method}'."); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + public static void OpenAndSave(string sourcePath, string outputStem) |
| 24 | + { |
| 25 | + using var engine = new ExcelEngine(); |
| 26 | + IApplication application = engine.Excel; |
| 27 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 28 | + IWorkbook workbook = application.Workbooks.Open(sourcePath); |
| 29 | + using FileStream stream = File.Create(outputStem + ".xlsx"); |
| 30 | + workbook.SaveAs(stream); |
| 31 | + workbook.Close(); |
| 32 | + } |
| 33 | + |
| 34 | + public static void CreateAndSave(DataTable table, string outputStem) |
| 35 | + { |
| 36 | + using var engine = new ExcelEngine(); |
| 37 | + IApplication application = engine.Excel; |
| 38 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 39 | + IWorkbook workbook = application.Workbooks.Create(1); |
| 40 | + IWorksheet sheet = workbook.Worksheets[0]; |
| 41 | + WriteGrid(sheet, table); |
| 42 | + using FileStream stream = File.Create(outputStem + ".xlsx"); |
| 43 | + workbook.SaveAs(stream); |
| 44 | + workbook.Close(); |
| 45 | + } |
| 46 | + |
| 47 | + public static void ImportDataTable(DataTable table, string outputStem) |
| 48 | + { |
| 49 | + using var engine = new ExcelEngine(); |
| 50 | + IApplication application = engine.Excel; |
| 51 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 52 | + IWorkbook workbook = application.Workbooks.Create(1); |
| 53 | + workbook.Worksheets[0].ImportDataTable(table, true, 1, 1); |
| 54 | + using FileStream stream = File.Create(outputStem + ".xlsx"); |
| 55 | + workbook.SaveAs(stream); |
| 56 | + workbook.Close(); |
| 57 | + } |
| 58 | + |
| 59 | + public static void ExportDataTable(string sourcePath, int expectedRows) |
| 60 | + { |
| 61 | + using var engine = new ExcelEngine(); |
| 62 | + IApplication application = engine.Excel; |
| 63 | + IWorkbook workbook = application.Workbooks.Open(sourcePath); |
| 64 | + DataTable exported = workbook.Worksheets[0].ExportDataTable(workbook.Worksheets[0].UsedRange, ExcelExportDataTableOptions.ColumnNames); |
| 65 | + BenchmarkHost.EnsureRows(exported.Rows.Count, expectedRows); |
| 66 | + workbook.Close(); |
| 67 | + } |
| 68 | + |
| 69 | + public static void ExcelToCsv(string sourcePath, string outputStem) |
| 70 | + { |
| 71 | + using var engine = new ExcelEngine(); |
| 72 | + IApplication application = engine.Excel; |
| 73 | + IWorkbook workbook = application.Workbooks.Open(sourcePath); |
| 74 | + workbook.SaveAs(outputStem + ".csv", ","); |
| 75 | + workbook.Close(); |
| 76 | + } |
| 77 | + |
| 78 | + public static void ExcelToJson(string sourcePath, string outputStem) |
| 79 | + { |
| 80 | + using var engine = new ExcelEngine(); |
| 81 | + IApplication application = engine.Excel; |
| 82 | + IWorkbook workbook = application.Workbooks.Open(sourcePath); |
| 83 | + workbook.SaveAsJson(outputStem + ".json", workbook.Worksheets[0]); |
| 84 | + workbook.Close(); |
| 85 | + } |
| 86 | + |
| 87 | + public static void ExcelToPdf(string sourcePath, string outputStem) |
| 88 | + { |
| 89 | + using var engine = new ExcelEngine(); |
| 90 | + IApplication application = engine.Excel; |
| 91 | + IWorkbook workbook = application.Workbooks.Open(sourcePath); |
| 92 | + var renderer = new XlsIORenderer(); |
| 93 | + using PdfDocument pdf = renderer.ConvertToPDF(workbook); |
| 94 | + using FileStream stream = File.Create(outputStem + ".pdf"); |
| 95 | + pdf.Save(stream); |
| 96 | + workbook.Close(); |
| 97 | + } |
| 98 | + |
| 99 | + static void WriteGrid(IWorksheet sheet, DataTable table) |
| 100 | + { |
| 101 | + int columns = table.Columns.Count; |
| 102 | + for (int c = 0; c < columns; c++) |
| 103 | + { |
| 104 | + sheet.SetValue(1, c + 1, table.Columns[c].ColumnName); |
| 105 | + } |
| 106 | + |
| 107 | + for (int r = 0; r < table.Rows.Count; r++) |
| 108 | + { |
| 109 | + int row = r + 2; |
| 110 | + DataRow dataRow = table.Rows[r]; |
| 111 | + for (int c = 0; c < columns; c++) |
| 112 | + { |
| 113 | + sheet.SetValue(row, c + 1, dataRow[c] is double amount ? amount.ToString(System.Globalization.CultureInfo.InvariantCulture) : (string)dataRow[c]); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments