[Two in one] C # Reading and exporting Excel class libraries (multiple sheets can be operated)

Source: Internet
Author: User
An Improved Excel class can be used to read Excel files or export content to excel files. It is easier to operate multiple sheet files.
You do not need to install any Excel files.
1. Read.

Read
Public Void Read ( String Filename)
{
Applibrary. readexcel. Workbook workbook =   Null ;
Workbook = Applibrary. readexcel. Workbook. getworkbook (filename );
Int K =   0 ;
System. Text. stringbuilder sb =   New System. Text. stringbuilder ();
Foreach (Applibrary. readexcel. Sheet In Workbook. Sheets)
{
K ++ ;
SB. append ( " Current is "   + K +   " Sheet <br/> " );
SB. append ( " <Table class = 'scroll 'cellpadding = 0 cellspacing = 0> " );
For ( Int Irow =   0 ; Irow < Sheet. Rows; irow ++ )
{
SB. append ( " <Tr> " );
SB. append ( " <TD> " );
SB. append ( " Current is "   + (Irow +   1 ) +   " Line " );
SB. append ( " </TD> " );
For ( Int Icol =   0 ; Icol < Sheet. columns; icol ++ )
{
Applibrary. readexcel. Cell cell = Sheet. getcell (icol, irow );
Object Val = Cell. value;

String Cellvalue = (Val ! =   Null ) ? Val. tostring (): "" ;

SB. append ( " <TD> " );
SB. append (cellvalue );
SB. append ( " </TD> " );

}
SB. append ( " </Tr> " );
}
SB. append ( " </Table> <br/> " );
}
Response. Write (sb. tostring ());

}

Read Excel:

After reading:

 

Export:

Export
Public   Void Toexcel ()
{
Applibrary. writeexcel. xlsdocument Doc =   New Applibrary. writeexcel. xlsdocument ();
Doc. filename =   " Report.xls " ;
String Sheetname =   String . Empty;
// Number of records
Int Mcount =   55 ;
// Quantity of each sheet
Int Inv =   10 ;
// Calculate the current number of sheets
Int K = Convert. toint32 (math. Round (convert. todouble (mcount / INV ))) +   1 ;

For ( Int I =   0 ; I < K; I ++ )
{
Sheetname =   " Current is sheet "   + I. tostring ();
Applibrary. writeexcel. worksheet Sheet = Doc. Workbook. worksheets. Add (sheetname );
Applibrary. writeexcel. Cells = Sheet. cells;
// Header of the first line
Cells. Add ( 1 , 1 , " Serial number " );
Cells. Add ( 1 , 2 , " Admission Ticket No. " );
Cells. Add ( 1 , 3 , " Examinee name " );
Cells. Add ( 1 , 4 , " Gender " );
Cells. Add ( 1 , 5 , " Major " );
Int F =   1 ;
For ( Int M = I * Inv; m < Mcount && M < (I +   1 ) * Inv; m ++ )
{
F ++ ;
Cells. Add (F, 1 , F -   1 );
Cells. Add (F, 2 , F );
Cells. Add (F, 3 , " Woody. Wu " );
Cells. Add (F, 4 , " Male " );
Cells. Add (F, 5 , " Economics " );
}
}
Doc. Send ();
Response. Flush ();
Response. End ();
}

 

 

DEMO code
Library source code

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.