Spire.XLS常見問題解答
為方便使用者快速掌握和了解Spire.XLS,本文列舉了Excel處理控件Spire.XLS常見問題及解答歡迎下載最新版體驗(yàn)!
技術(shù)交流社群:767755948
如何將圖像添加到 Excel 中?
A : 請調(diào)用 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 : 請調(diào)用方法 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 中設(shè)置打印區(qū)域?
A:您可以設(shè)置工作表的 PrintArea 屬性來執(zhí)行此操作。完整代碼:
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");
如何復(fù)制帶格式的單元格?
A:Spire.XLS 為您提供了一種名為 Copy 的方法來復(fù)制帶有格式的單元格。完整代碼:
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 轉(zhuǎn)換為 PDF?
A : 首先,您需要將參考 Spire.Pdf 和 Spire.Common 添加到您的項(xiàng)目中。然后創(chuàng)建一個(gè) PdfDocument 實(shí)例和一個(gè) PDF 轉(zhuǎn)換器對象。最后,調(diào)用 Convert 方法將 XLS 轉(zhuǎn)換為 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 為您提供了一個(gè)名為 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 為您提供了一個(gè)名為 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 中設(shè)置指定范圍內(nèi)的數(shù)字格式?
A : 您可以使用目標(biāo)單元格的屬性 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 : 只需設(shè)置單元格的公式屬性即可添加公式。完整代碼:
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);
如何合并多個(gè)工作簿?
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");