public static void ParseXlsToPro(string xmlFile) { if (!xmlFile.ToLower().Contains("cfg_")) { return; } xmlFile = xmlFile.Replace("\\", "/"); JCExcel excel = new JCExcel(xmlFile); }
public static void TranslateCsvToXls(string filePath, string outFolder) { FileInfo file = new FileInfo(filePath); if (file.Extension != ".csv") { return; } string name = file.Name.Substring(0, file.Name.LastIndexOf('.')); FileInfo fileCopy = new FileInfo(filePath.Replace(".csv", ".xml")); using (StreamReader sr = new StreamReader(filePath, Encoding.UTF8, false)) { using (StreamWriter sw = new StreamWriter(fileCopy.FullName, false, Encoding.Unicode)) { sw.Write(sr.ReadToEnd()); } } //载入csv文档 Workbook workbook = new Workbook(); workbook.LoadFromFile(fileCopy.FullName, ",", 1, 1); string outFile = outFolder + "\\" + name + ".xls"; //保存为xls格式 workbook.SaveToFile(outFile, ExcelVersion.Version97to2003); fileCopy.Delete(); JCExcel excecl = new JCExcel(outFile); excecl.ToExcelTwo(excecl.dataSet, outFile, outFile); // excecl.DataSetToExcel(excecl.dataSet, outFile); if (!excecl.CheckColumnContent()) { MessageBox.Show(string.Format("{0}失败", name)); File.Delete(outFile); } if (!excecl.DetectionOfDigits()) { MessageBox.Show(string.Format("{0}失败", name)); File.Delete(outFile); } }
public static void ParseXlsToPrto(string xmlFile, string outDirectory) { if (!xmlFile.ToLower().Contains("cfg_")) { return; } xmlFile = xmlFile.Replace("\\", "/"); DataRowCollection data = ExcelAccess.ReadExcel(xmlFile, 0, ExcelAccess.ExcelType.xls); string name = Path.GetFileNameWithoutExtension(xmlFile); JCExcel excel = new JCExcel(xmlFile); StringBuilder sb = new StringBuilder(); sb.AppendLine("package TABLE;"); sb.AppendLine("import \"table_common.proto\";"); sb.AppendLine(""); sb.AppendLine(string.Format("message {0}", name.ToUpper())); sb.AppendLine("{"); ContentType type = ContentType.None; string typeName = string.Empty; string protoName = string.Empty; string typeProperty = string.Empty; List <string> idLidst = new List <string>(); for (int i = 0; i < excel.ColumnCount; i++) { if (excel.IsConformTypeNameNorm(i, out type, out protoName, out typeProperty)) { if (type == ContentType.stringValue) { typeName = "string"; } else if (type == ContentType.floatValue) { typeName = "float"; } else { typeName = type.ToString(); } //每列数据的检查 if (!excel.CheckColumnContent(i, type)) { continue; } if (string.IsNullOrEmpty(protoName)) { continue; } //避免同名的出现 if (idLidst.Contains(protoName)) { MessageBox.Show(string.Format("{0} {1}列出现重复名称{2}", xmlFile, i + 1, protoName)); continue; } idLidst.Add(protoName); if (excel.GetTitle(i) == "合并字段") { sb.AppendLine(" //" + excel.GetDesc(i)); sb.AppendLine(string.Format("\t{0}{1} {2} {3} = {4};", (excel.IsClientUse(i) ? "" : "//"), "required", typeName, protoName, i + 1)); } else { sb.AppendLine(string.Format("\t{0}{1} {2} {3} = {4};//{5} {6}", (excel.IsClientUse(i) ? "" : "//"), typeProperty, typeName, protoName, i + 1, excel.GetTitle(i), excel.GetDesc(i))); } } } sb.AppendLine("}"); sb.AppendLine(""); sb.AppendLine(string.Format("message {0}", name.ToUpper() + "ARRAY")); sb.AppendLine("{"); sb.AppendLine(string.Format("\trepeated {0} rows = 1;", name.ToUpper())); sb.AppendLine("}"); Utility.SaveFileContent(outDirectory + "/c_table_" + name.ToLower() + ".proto", sb.ToString()); }