|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Windows.Forms; |
| 7 | +using NPOI.SS.UserModel; |
| 8 | +using TableML.Compiler; |
| 9 | + |
| 10 | +namespace TableMLGUI |
| 11 | +{ |
| 12 | + public class ExcelHelper |
| 13 | + { |
| 14 | + public static void UpdateAllTableSyntax() |
| 15 | + { |
| 16 | + var findPath = System.Environment.CurrentDirectory + ".\\..\\Src"; |
| 17 | + //var findPath = @"e:\3dsn\plan\005ConfigTable\Src\"; |
| 18 | + var files = Directory.GetFiles(findPath, "*.xls", SearchOption.AllDirectories); |
| 19 | + Console.WriteLine("开始更新{0}张Excel表", files.Length); |
| 20 | + var count = 0; |
| 21 | + foreach (string file in files) |
| 22 | + { |
| 23 | + var update = ToCSharpSyntax(file); |
| 24 | + if (update) count += 1; |
| 25 | + } |
| 26 | + Console.WriteLine("更新Excel表完成。"); |
| 27 | + MessageBox.Show(string.Format("共整理{0}张表的前端字段为C#类型", count), "整理完成", MessageBoxButtons.OK, MessageBoxIcon.Information); |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// 替换成C#的数据类型 |
| 32 | + /// </summary> |
| 33 | + /// <param name="filePath"></param> |
| 34 | + /// <returns>文件是否有改变</returns> |
| 35 | + public static bool ToCSharpSyntax(string filePath) |
| 36 | + { |
| 37 | + bool fileChange = false; |
| 38 | + IWorkbook Workbook; |
| 39 | + ISheet Worksheet; |
| 40 | + using (var file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) // no isolation |
| 41 | + { |
| 42 | + try |
| 43 | + { |
| 44 | + Workbook = WorkbookFactory.Create(file); |
| 45 | + } |
| 46 | + catch (Exception e) |
| 47 | + { |
| 48 | + throw new Exception(string.Format("无法打开Excel: {0}, 可能原因:正在打开?或是Office2007格式(尝试另存为)? {1}", filePath, |
| 49 | + e.Message)); |
| 50 | + //IsLoadSuccess = false; |
| 51 | + } |
| 52 | + } |
| 53 | + Worksheet = Workbook.GetSheetAt(0); |
| 54 | + List<ICell> cells = Worksheet.GetRow(4).Cells; |
| 55 | + foreach (ICell cell in cells) |
| 56 | + { |
| 57 | + |
| 58 | + if (cell.StringCellValue == "num") |
| 59 | + { |
| 60 | + cell.SetCellValue("int"); |
| 61 | + fileChange = true; |
| 62 | + } |
| 63 | + |
| 64 | + if (cell.StringCellValue == "str") |
| 65 | + { |
| 66 | + cell.SetCellValue("string"); |
| 67 | + fileChange = true; |
| 68 | + } |
| 69 | + if (cell.StringCellValue.StartsWith("arr")) |
| 70 | + { |
| 71 | + cell.SetCellValue("string"); |
| 72 | + fileChange = true; |
| 73 | + } |
| 74 | + if (cell.StringCellValue.StartsWith("ssg")) |
| 75 | + { |
| 76 | + cell.SetCellValue("string"); |
| 77 | + fileChange = true; |
| 78 | + } |
| 79 | + } |
| 80 | + //文件没有改变,不保存 |
| 81 | + if (!fileChange) return false; |
| 82 | + using (var memStream = new MemoryStream()) |
| 83 | + { |
| 84 | + Workbook.Write(memStream); |
| 85 | + memStream.Flush(); |
| 86 | + memStream.Position = 0; |
| 87 | + |
| 88 | + using (var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write)) |
| 89 | + { |
| 90 | + var data = memStream.ToArray(); |
| 91 | + fileStream.Write(data, 0, data.Length); |
| 92 | + fileStream.Flush(); |
| 93 | + } |
| 94 | + } |
| 95 | + return true; |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments