Spire.XLS常见问题解答
为方便使用者快速掌握和了解Spire.XLS,本文列举了Excel处理控件Spire.XLS常见问题及解答欢迎下载最新版体验!
技术交流社群:767755948
如何将图像添加到 Excel 中?
A : 请调用 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");
如何在 Excel 文件的工作表中插入新行?
A : 请调用方法 InsertRow 将新行添加到 Excel 文件的工作表中。完整代码:
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");
如何在 XLS 中设置打印区域?
A:您可以设置工作表的 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");
如何复制带格式的单元格?
A:Spire.XLS 为您提供了一种名为 Copy 的方法来复制带有格式的单元格。完整代码:
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[3, 2].Copy(sheet3.Range[6,3]); workbook.SaveToFile("result.xlsx");
如何将 XLS 转换为 PDF?
A : 首先,您需要将参考 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");
如何在 XLS 中合并单元格?
A:Spire.XLS 为您提供了一种称为 Merge 的方法来合并 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");
如何重新排列 XLS 文件中的工作表?
A : 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");
如何删除excel工作表中的列?
A: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");
如何在 XLS 中设置指定范围内的数字格式?
A : 您可以使用目标单元格的属性 NumberFormat 来格式化 cellrange 的 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");
如何在单元格中添加公式?
A : 只需设置单元格的公式属性即可添加公式。完整代码:
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);
如何合并多个工作簿?
A: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");