Excel processing control spire Answers to XLS hot questions

To facilitate users to quickly master and understand spire Xls, the following small series summarizes and arranges a series of common problems in the trial process of probationers, hoping to help you!
---------------<<Spire. Download the latest version of xls>> 

(1) How do I add an image to Excel?

A: please call the Add method to Add the image to the worksheet of the Excel file. Full code:

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");

(2) How do I insert a new row into a worksheet in an Excel file?

A: please call the method InsertRow to add a new row to the worksheet of the Excel file. Full code:

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");

(3) How to set the print area in XLS?

A: You can do this by setting the PrintArea property of the worksheet. Full code:

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");

(4) How do I copy formatted cells?

A: Spire.XLS provides you with a method called Copy to Copy formatted cells. Full code:

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");

(5) How do I convert XLS to PDF?

A: first, you need to refer to spire PDF and spire Add common to your project. Then create a PdfDocument instance and a PDF Converter object. Finally, the Convert method is called to convert XLS to PDF. Full code:

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");

(6) How do I merge cells in XLS?

A: Spire.XLS provides you with a method called Merge to Merge cells in XLS. Full code:

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");

(7) How do I rearrange worksheets in XLS files?

A : Spire.XLS provides you with a method called MoveWorksheet to rearrange worksheets in XLS files? Full code:

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");

(8) How to delete columns in excel worksheet?

A: Spire.XLS provides you with a method called DeleteColumn to delete columns. This method immediately affects the order of the worksheet collection. Full code:

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");

(9) How to set the number format within the specified range in XLS?

A: you can use the attribute NumberFormat of the target cell to format the NumberValue of the cellrange. Full code:

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");

(10) How to add a formula to a cell?

A: just set the formula attribute of the cell to add a formula. Full code:

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);

(11) How do I merge multiple workbooks?

A: Spire.XLS provides you with a method called AddCopy to merge Excel files. Full code:

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");

If you have other questions, you can leave a message in the comment area ~ ~ Xiaobian will reply to you as soon as you see it~~

Keywords: Java C++ C# .NET linq

Added by simun on Wed, 02 Mar 2022 05:17:11 +0200