Usage of Microsoft.Office.Interop.Excel and using Microsoft.Office.Interop.Excel to convert Web pages to PDF

Source: Internet
Author: User

1. Common usage

Using Microsoft.Office.Interop.Excel;

1) Create a new Excel ApplicationClass Excelapp = new ApplicationClass ();
Microsoft.Office.Interop.Excel.Workbook book = ExcelApp.Workbooks.Add ();
2) Change the Excel title bar:
Excelapp.caption: = ' application calls Microsoft Excel ';
3) Add a new workbook:
EXCELAPP.WORKBOOKS.ADD;
4) Open a workbook that already exists:
ExcelApp.WorkBooks.Open (' C:\Excel\Demo.xls ');
5) Set the 2nd worksheet as the active sheet:
EXCELAPP.WORKSHEETS[2]. Activate; Or
excelapp.workssheets[' Sheet2 '. Activate;
6) Assign a value to the cell:
excelapp.cells[1,4]. Value: = ' first row fourth column ';
7) Set the width of the specified column (in number of characters), in the first column as an example:
EXCELAPP.ACTIVESHEET.COLUMNS[1]. Columnswidth: = 5;
8) Set the height of the specified line (in points) (1 lbs = 0.035 cm), with the second behavior example:
EXCELAPP.ACTIVESHEET.ROWS[2]. RowHeight: = 1/0.035; 1 cm
9) Insert a page break before line 8th:
EXCELAPP.WORKSHEETS[1]. ROWS[8]. PageBreak: = 1;
10) Delete the page break before the 8th column:
EXCELAPP.ACTIVESHEET.COLUMNS[4]. PageBreak: = 0;
11) Specify the width of the border line:
excelapp.activesheet.range[' B3:d4 '. BORDERS[2]. Weight: = 3;
1-Left 2-right 3-top 4-bottom 5-oblique (\) 6-oblique (/)
12) Clear the cell Formula for column Fourth of the first row:
excelapp.activesheet.cells[1,4]. clearcontents;
13) Set the first line font properties:
EXCELAPP.ACTIVESHEET.ROWS[1]. Font.Name: = ' official script ';
EXCELAPP.ACTIVESHEET.ROWS[1]. Font.Color: = Clblue;
EXCELAPP.ACTIVESHEET.ROWS[1]. Font.Bold: = True;
EXCELAPP.ACTIVESHEET.ROWS[1]. Font.underline: = True;
14) make Page setup:
A. Header:
ExcelApp.ActiveSheet.PageSetup.CenterHeader: = ' report presentation ';
B. Footer:
ExcelApp.ActiveSheet.PageSetup.CenterFooter: = ' &p page ';
C. Header to top margin 2cm:
ExcelApp.ActiveSheet.PageSetup.HeaderMargin: = 2/0.035;
D. Footer End margin 3cm:
ExcelApp.ActiveSheet.PageSetup.HeaderMargin: = 3/0.035;
E. Top margin 2cm:
ExcelApp.ActiveSheet.PageSetup.TopMargin: = 2/0.035;
F. Bottom margin 2cm:
ExcelApp.ActiveSheet.PageSetup.BottomMargin: = 2/0.035;
G. Left margin 2cm:
ExcelApp.ActiveSheet.PageSetup.LeftMargin: = 2/0.035;
H. Right margin 2cm:
ExcelApp.ActiveSheet.PageSetup.RightMargin: = 2/0.035;
I. The page is centered horizontally:
ExcelApp.ActiveSheet.PageSetup.CenterHorizontally: = 2/0.035;
J. The page is centered vertically:
ExcelApp.ActiveSheet.PageSetup.CenterVertically: = 2/0.035;
K. Print the Cell network cable:
ExcelApp.ActiveSheet.PageSetup.PrintGridLines: = True;
15) Copy operation:
A. Copy the entire worksheet:
ExcelApp.ActiveSheet.Used.Range.Copy;
B. Copy the specified area:
excelapp.activesheet.range[' A1:e2 '. Copy;
C. Start pasting from A1 position:
ExcelApp.ActiveSheet.Range. [' A1 ']. PasteSpecial;
D. Start pasting from the tail of the file:
ExcelApp.ActiveSheet.Range.PasteSpecial;
16) Insert one row or column:
A. excelapp.activesheet.rows[2]. Insert;
B. excelapp.activesheet.columns[1]. Insert;
17) Delete one row or column:
A. excelapp.activesheet.rows[2]. Delete;
B. excelapp.activesheet.columns[1]. Delete;
18) Print Preview worksheet:
ExcelApp.ActiveSheet.PrintPreview;
19) Print out worksheet:
ExcelApp.ActiveSheet.PrintOut;
20) Worksheet Save:
If not ExcelApp.ActiveWorkBook.Saved then
ExcelApp.ActiveSheet.PrintPreview;
21) Save the worksheet as:
Excelapp.saveas (' C:\Excel\Demo1.xls ');
22) Discard the disk:
ExcelApp.ActiveWorkBook.Saved: = True;
23) Close Workbook:
ExcelApp.WorkBooks.Close;
24) Exit Excel:
Excelapp.quit;
2. Use Microsoft.Office.Interop.Excel to convert Web pages to PDF
1) First save the Web page HTML as an Excel file
StringWriter HTML =NewStringWriter (); HtmlTextWriter TW=NewHtmlTextWriter (HTML);Base. Render (TW);//Excelname to store Excel addressesFileStream fs =NewFileStream (Excelname, FileMode.Create); StreamWriter SW=NewStreamWriter (FS, encoding.getencoding ("Utf-8")); //Start WritingSW.            Write (HTML); //emptying buffersSW.            Flush (); //Close the streamSW.            Close (); Fs. Close ();

2) then use Microsoft.Office.Interop.Excel to turn Excel into PDF

Microsoft.Office.Interop.Excel.dll is referenced first, and setting the DLL's property value cannot be embedded in an interop type of false. Otherwise, the type "Microsoft.Office.Interop.Excel.ApplicationClass" undefined constructor cannot embed the interop type " Microsoft.Office.Interop.Excel.ApplicationClass ". Please use the appropriate interface error instead.

Then install SaveAsPDFandXPS.exe (install this plugin to save Excel as a pdf)

The source code is as follows:

/// <summary>        ///Excel save PDF/// </summary>        /// <param name= "Excelpath" >Excel full path</param>        /// <param name= "Pdfpath" >PDF Save Path</param>        /// <returns></returns>         Public Static BOOLCovertexceltopdf (stringExcelpath,stringPdfpath) {            ObjectMissing =Type.            Missing; ////Create an Excel application instanceApplicationClass application =NULL ; ////Create a workbook instanceWorkbook Workbook =NULL ; Try{Application=NewApplicationClass (); ////Open WorkbookWorkBook =application.                                                      Workbooks.Open (Excelpath, missing, missing, missing, missing, missing,                Missing, missing, missing, missing, missing, missing, missing, missing, missing); ////Open SheetWorksheet ws = (Worksheet) workbook.worksheets.item[1]; ////Set print and put to horizontalWs. Pagesetup.orientation =xlpageorientation. xlportrait; ////Set the Excel content to be displayed on one page when printing. Zoom must be set to FalseWs. Pagesetup.zoom =false ; Ws. Pagesetup.fittopagestall=1; Ws. Pagesetup.fittopageswide=1; ////Publish workbook to PDF or XPS formatws.                    ExportAsFixedFormat (Xlfixedformattype xltypepdf, Pdfpath, xlfixedformatquality. Xlqualitystandard , true                    , false     ////Ignore Print area, missing, missing, missing, missing); return true ; }            Catch            {                Throw ; }            finally            {                ////Workbook closed                if(WorkBook! =NULL) {Workbook.close (true, missing, missing); WorkBook=NULL ; }                ////Excel application exits close                if(Application! =NULL) {application.                    Quit (); Application=NULL ; } GC.                Collect (); Gc.                WaitForPendingFinalizers (); Gc.                Collect (); Gc.            WaitForPendingFinalizers (); }        }

Non-original, reproduced summary

Http://www.cnblogs.com/ttrjba/p/3873538.html

Http://www.cnblogs.com/yuteng/articles/1753767.html

https://msdn.microsoft.com/zh-cn/library/ms173186 (vs.80). aspx

Usage of Microsoft.Office.Interop.Excel and using Microsoft.Office.Interop.Excel to convert Web pages to PDF

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.