C # process excel, PDF, and access

Source: Internet
Author: User
PDF:

Icsharpcode. sharpziplib. dll and itextsharp. dll are used to write a PDF file)

Code

 
Using itextsharp. text;
Using itextsharp.text.pdf;
//..
// Degree of attention in the table ‑ box
Private float mytableborderwidth = 0.01f;
// Use a body
Private string myfontdir = @ "C: \ WINDOWS \ fonts \ mingliu. TTC, 0 ";
//.
Basefont bfsun = basefont. createfont (this. myfontdir, basefont. identity_h, basefont. not_embedded );
Itextsharp. Text. Font documentfont = new itextsharp. Text. Font (bfsun, 4, itextsharp. Text. Font. Normal );
Document tmpdocument = new document (pagesize. a4.rotate (); // horizontal
Tmpdocument. setmargins (6f, 6f, 20f, 20f); // you can specify the bottom margin.
Using writer upload writer = Using writer. getinstance (tmpdocument, new filestream (this. outputdir + pdffilename + ". pdf", filemode. Create ));
// Set the footer
Headerfooter footer = new headerfooter (new phrase ("page", documentfont), true );
Footer. Alignment = element. align_center;
Footer. Border = itextsharp. Text. Rectangle. no_border;
Tmpdocument. footer = footer;
Tmpdocument. open ();
Paragraph mytitle = new paragraph ("Subject 123321 \ n", documentfont );
Mytitle. Alignment = element. align_center;
Tmpdocument. Add (mytitle );
// Skip the code used to determine whether the code is paging Based on the Data Volume
Table atable = new table (24, (hasmultipages? This. onepagecontentnum: totalrownum) + 1 );
Atable. Border = itextsharp. Text. Rectangle. Left | itextsharp. Text. Rectangle. bottom;
Atable. borderwidth = This. mytableborderwidth;
Atable. widths = new float [] {12f, 12f, 15f, 18f, 12f, 12f, 12f, 15f, 18f, 12f,
15f, 12f, 18f, 18f, 15f, 12f, 18f, 18f, 15f, 15f,
15f, 15f, 12f, 12f };
Atable. autofillemptycells = true; // set auto-Fill
// Set the table content
Cell tmpcell = new cell (new paragraph (tmpcelltext + "\ n", documentfont ));
Tmpcell. horizontalalignment = element. align_center;
Tmpcell. Border = itextsharp. Text. Rectangle. Top | itextsharp. Text. Rectangle. Right;
Tmpcell. borderwidth = This. mytableborderwidth;
Atable. addcell (tmpcell, new point (I, POS ));
//.
Tmpdocument. Add (atable );
Tmpdocument. Newpage (); // when appropriate, flip this sentence
// Close the PDF
Tmpdocument. Add (atable );

 

Read access: Code
String strcon = "provider = Microsoft. Jet. oledb.4.0;" + @ "Data Source = G: \ abinxm \ 123.mdb ";
Using (oledbconnection con = new oledbconnection (this. strcon ))
{
Con. open ();
Oledbcommand CM = new oledbcommand (strsql, con );
Oledbdatareader reader = cm. executereader ();
While (reader. Read ())
{
String strtmpidcard = reader [0]. tostring ();
//
}
// Finish finishing the job
Reader. Close ();
Con. Close ();
Read Excel: Introduce Microsoft. Office. InterOP. Excel. dll first code
Using Microsoft. Office. InterOP. Excel;

Microsoft. Office. InterOP. Excel. Application excelobj = new Microsoft. Office. InterOP. Excel. Application ();
Object missing = type. missing;
Microsoft. Office. InterOP. Excel. Workbook theworkbook = excelobj. workbooks. Open (
This. xlwfileurl, missing,
Missing, missing, and missing );
Microsoft. Office. InterOP. Excel. Sheets sheets = theworkbook. worksheets;
Microsoft. Office. InterOP. Excel. worksheet datasheet = NULL;
Foreach (Microsoft. Office. InterOP. Excel. worksheet sheet in sheets ){
If (sheet. Name = textboxsheetname) {datasheet = sheet; break ;}
}
If (null = datasheet)
{
MessageBox. Show (this, "No sheet." named "+ textboxsheetname + .");
Return;
}
Int totalrow_num = datasheet. Rows. count;
// Read an object in the dead format. The header is no longer needed.
For (INT I = 2; I <= totalrow_num; I ++ ){
// Read the value
Object obj_id_card = datasheet. get_range (datasheet. cells [I, 3], datasheet. cells [I, 3]). value2;
Microsoft. Office. InterOP. Excel. Range = datasheet. get_range (datasheet. cells [I, 2], datasheet. cells [I, 25]);
}
// Execute so many resources to release, or lock the Excel file to read ~ Nnd
Datasheet = NULL;
Sheets = NULL;
Theworkbook = NULL;
Excelobj = NULL;
GC. Collect ();
GC. waitforpendingfinalizers ();
GC. Collect ();

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.