/// <summary> /// Convert excel to html with picture files /// </summary> /// <param name="inputFileName">Absolute name of input file</param> /// <param name="outputFolder">Folder for saving results</param> public static void ConvertToHtml(string inputFileName, string outputFolder, Dictionary <string, string> parameters) { Spire.Xls.Workbook workbook = new Spire.Xls.Workbook(); workbook.LoadFromFile(inputFileName); int excelMaxRows = GetIntValue(parameters, Parameter.ExcelMaxRows.Value); int excelMaxColumns = GetIntValue(parameters, Parameter.ExcelMaxColumns.Value); int excelMaxSheetSize = GetIntValue(parameters, Parameter.ExcelMaxSheetSize.Value); foreach (Spire.Xls.Worksheet sheet in workbook.Worksheets) { int lastRow = sheet.LastRow; int lastColumn = sheet.LastColumn; lastRow = excelMaxRows > 0 && lastRow > excelMaxRows ? excelMaxRows : lastRow; lastRow = excelMaxSheetSize > 0 && lastRow > excelMaxSheetSize ? excelMaxSheetSize : lastRow; lastColumn = excelMaxColumns > 0 && lastColumn > excelMaxColumns ? excelMaxColumns : lastColumn; lastColumn = excelMaxSheetSize > 0 && lastColumn > excelMaxSheetSize ? excelMaxSheetSize : lastColumn; sheet.LastRow = lastRow; sheet.LastColumn = lastColumn; using (var stream = new MemoryStream()) { sheet.SaveToHtml(stream); string result = System.Text.Encoding.UTF8.GetString(stream.ToArray()); if (result.IndexOf(replaceValueExcel) >= 0) { result = result.Replace(replaceValueExcel, ""); } else if (result.IndexOf(replaceValueWithoutTag) >= 0) { result = result.Replace(replaceValueWithoutTag, ""); } else if (result.IndexOf(replaceValue) >= 0) { result = result.Replace(replaceValue, ""); } //result = SetBorder(result, 1); if (workbook.Worksheets.Count > 1) { if (result.IndexOf("<td") >= 0) { File.WriteAllText(outputFolder + "/" + new FileInfo(inputFileName).Name + "." + sheet.Name + ".html", result, Encoding.UTF8); } } else { if (result.IndexOf("<td") >= 0) { File.WriteAllText(outputFolder + "/" + new FileInfo(inputFileName).Name + ".html", result, Encoding.UTF8); } } result = null; sheet.Dispose(); } } workbook.Dispose(); }
/// <summary> /// 添加材料 /// </summary> /// <param name="input"></param> /// <returns></returns>between types 'System.String' and 'System.Nullable`1[System.Guid]'.” public IListResult <MaterialDto> AddMaterial(string input) { try { // var material = new Material // { // Name = input.Name, // Model = input.Model, // Strength = input.Strength, // MaterialType = input.MaterialType, // Date = input.Date, // TypicalPartId = input.TypicalPartId, // ManufactoryId = input.ManufactoryId, // FileString = input.FileString //}; //创建Workbook对象 Workbook wb = new Workbook(); //加载Excel文档//@"C:\Users\Administrator\Desktop\新建 XLSX 工作表.xlsx" wb.LoadFromFile(input); //获取第一个工作表 Worksheet sheet = wb.Worksheets[0]; //获取指定单元格的值 string value = sheet.Range["A2"].Value2.ToString(); //自定义时间格式 DateTimeFormatInfo dtFormat = new DateTimeFormatInfo(); dtFormat.ShortDatePattern = "yyyy/MM/dd"; var material = new MaterialDto { Name = sheet.Range["A2"].Value2.ToString(), //Model = sheet.Range["C2"].Value2.ToString(), TypicalPartId = new Guid(sheet.Range["a2"].Value2.ToString()), //Date= Convert.ToDateTime(sheet.Range["b2"].Value2.ToString(), dtFormat)//注意:string格式有要求,yyyy/MM/dd }; //释放资源 wb.Dispose(); //await _materialRepository.InsertAsync(material); return(null); } catch (Exception) { return(null); } }