发布时间:2020-01-13编辑:佚名阅读(1771)
No.1 如何将图像添加到Excel中?
答:请调用Add方法将图像添加到Excel文件的工作表中。完整代码:
Workbook workbook = new Workbook(); Worksheet sheet = workbook.Worksheets[0]; //insert the picture day.jpg into the sheet and place it in the cell "B3" sheet.Pictures.Add(3, 2, "day.jpg"); workbook.SaveToFile("result.xlsx");
No.2 如何将新行插入Excel文件的工作表中?
答:请调用InsertRow方法将新行添加到Excel fils的工作表中。完整代码:
Workbook workbook = new Workbook(); workbook.LoadFromFile("sample.xlsx"); Worksheet sheet = workbook.Worksheets[0]; //add a new row in the third row sheet.InsertRow(3); workbook.SaveToFile("result.xlsx");
No.3 如何在XLS中设置打印区域?
答:您可以设置工作表的PrintArea属性来执行此操作。完整代码:
Workbook workbook = new Workbook(); workbook.LoadFromFile("sample.xlsx"); Worksheet sheet = workbook.Worksheets[0]; //set print area from cell "B2" to cell "F8" sheet.PageSetup.PrintArea = "B2:F8"; workbook.SaveToFile("result.xlsx");
No.4 如何使用格式复制单元格?
答:Spire.XLS为您提供了一种称为“复制”的方法,用于格式化复制单元格。完整代码:
Workbook workbook = new Workbook(); workbook.LoadFromFile("sample.xlsx"); Worksheet sheet1 = workbook.Worksheets[0]; Worksheet sheet3 = workbook.Worksheets[2]; //copy cell "B2" in sheet1 to cell "C6" in sheet3 sheet1.Range[2, 2].Copy(sheet3.Range[6,3]); workbook.SaveToFile("result.xlsx");
No.5 如何将XLS转换为PDF?
答:首先,您需要将Spire.Pdf和Spire.Common引用添加到您的项目中。然后创建一个PdfDocument和一个PDF转换器对象的实例。最后,调用方法Convert将XLS转换为PDF。完整代码:
Workbook workbook = new Workbook(); workbook.LoadFromFile("sample.xlsx"); PdfConverter pdfConverter = new PdfConverter(workbook); PdfDocument pdfDocument = new PdfDocument(); //settings of result PDF docement pdfDocument.PageSettings.Orientation = PdfPageOrientation.Landscape; pdfDocument.PageSettings.Width = 970; pdfDocument.PageSettings.Height = 850; PdfConverterSettings settings = new PdfConverterSettings(); settings.TemplateDocument = pdfDocument; //convert XLS to PDF using PdfConverter pdfDocument = pdfConverter.Convert(settings); pdfDocument.SaveToFile("result.pdf");
No.6 如何合并XLS中的单元格?
答:Spire.XLS为您提供了一种称为合并的方法来合并XLS中的单元格。完整代码:
Workbook workbook = new Workbook(); workbook.LoadFromFile("sample.xlsx"); Worksheet sheet = workbook.Worksheets[0]; //merges cells "B3" "B4" sheet.Range["B3:B4"].Merge(); workbook.SaveToFile("result.xlsx");
No.7 如何在XLS文件中重新排列工作表?
答:Spire.XLS为您提供了一种名为MoveWorksheet的方法来重新排列XLS文件中的工作表完整代码:
Workbook workbook = new Workbook(); workbook.LoadFromFile("sample.xlsx"); Worksheet sheet = workbook.Worksheets[3]; //move the fourth worksheet sheet to the first position of the worksheets sheet.MoveWorksheet(0); workbook.SaveToFile("result.xlsx");
No.8 如何删除excel工作表中的列?
答:Spire.XLS为您提供了一种名为DeleteColumn的方法来删除列。此方法立即影响工作表集合的顺序。完整代码:
Workbook workbook = new Workbook(); workbook.LoadFromFile("sample.xlsx"); Worksheet sheet = workbook.Worksheets[1]; //delete the second column sheet.DeleteColumn(2); //delete the fourth column sheet.DeleteColumn(3); workbook.SaveToFile("result.xlsx");
No.9 如何在XLS中指定范围内设置数字格式?
答:您可以使用目标单元格的属性NumberFormat来格式化单元格的NumberValue。完整代码:
Workbook workbook = new Workbook(); workbook.LoadFromFile("sample.xlsx"); Worksheet sheet = workbook.Worksheets[0]; //set number format in specified range sheet.Range[2, 2, 6, 6].NumberFormat = "$#,##0.00"; sheet.Range["C3"].NumberValue = 3240.689; sheet.Range["D4"].NumberValue = 5230.123; workbook.SaveToFile("result.xlsx");
No.10 如何将公式添加到单元格?
答:只需设置单元格的公式属性即可添加公式。完整代码:
Workbook workbook = new Workbook(); Worksheet worksheet = workbook.Worksheets[0]; //add formula =IF(H7>0,(IF(F7 > 0,(H7-F7)/F7,"""")),"""") to cell "J7" string formula = @"=IF(H7>0,(IF(F7 > 0,(H7-F7)/F7,"""")),"""")"; worksheet.Range["J7"].Formula = formula; worksheet.Range["F7"].NumberValue = 5; worksheet.Range["H7"].NumberValue = 4; workbook.SaveToFile("result.xlsx", ExcelVersion.Version2007);
No.11 如何合并多个工作簿?
答:Spire.XLS为您提供了一种名为AddCopy的方法来合并Excel文件。完整代码:
Workbook workbook = new Workbook(); workbook.LoadFromFile("sample1.xlsx"); Workbook workbookDest = new Workbook(); workbookDest.LoadFromFile("sample2.xlsx"); //merge workbook with workbookDest workbookDest.Worksheets.AddCopy(workbook.Worksheets); workbookDest.SaveToFile("result.xlsx");
0人
0人
0人
0人
博主,你好,spire在进行10000行以上数据设置单元格格式时耗时,内存增加达1G怎么解决,谢谢!QQ4064812
量大耗时耗内存正常,可以换其它方法看看。