Spire.XLS for .NET使用过程中的常见问题

作者:控件中国网   出处:控件中国网   2015-08-19 09:24:47   阅读:8

Spire.XLS for .NET是一款无需安装Microsoft Office Excel,就可以对Excel文件进行操作的文档处理控件,功能上几乎和Microsoft OfficeExcel一样,下面主要介绍该产品在使用过程中经常遇到的一些常见问题:

1.怎么添加一个图片到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");
2.怎么为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");
3.怎么设置Excel工作表的打印区域?
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.怎么复制带格式化的单元格?
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.怎么转换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");
6.怎么合并工作表中的单元格?
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.怎么重新排列Excel文件里的工作表?
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.怎么删除Excel工作表中的某一列?
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.怎么设置一定范围内单元格的格式?
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.怎么为Excel单元格设置公式?
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.怎么合并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");

Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat