Export any form of Excel (sharing experiences)

Source: Internet
Author: User
///


/// Generate an Excel file
/// _ Til is the table title, _ row is the number of rows in the table, _ col is the number of columns in the table, _ dt is the data table, and _ filename is the name of the saved Excel table.
///
Public Void Savetoexcel ( String _ Til, Int _ Row, Int _ Col, datatable _ DT, String _ Filename)
{
Microsoft. Office. InterOP. owc11.spreadsheetclass xlsheet = New Microsoft. Office. InterOP. owc11.spreadsheetclass ();
// Merge Cells
Xlsheet. get_range (xlsheet. cells [ 1 , 1 ], Xlsheet. cells [ 1 , _ Col]). set_mergecells ( True );
// Xlsheet. get_range (xlsheet. cells [2, 1], xlsheet. cells [3, 2]). set_mergecells (true );
Xlsheet. activesheet. cells [ 1 , 1 ] = _ Til;
// Bold font
Xlsheet. get_range (xlsheet. cells [ 1 , 1 ], Xlsheet. cells [ 1 , _ Col]). Font. set_bold ( True );
// Cell text align horizontally
Xlsheet. get_range (xlsheet. cells [ 1 , 1 ], Xlsheet. cells [_ row + 2 , _ Col]). set_horizontalalignment (Microsoft. Office. InterOP. owc11.xlhalign. xlhaligncenter );
// Set Font Size
Xlsheet. get_range (xlsheet. cells [ 1 , 1 ], Xlsheet. cells [ 1 , _ Col]). Font. set_size ( 14 );
// Set column width
// Xlsheet. get_range (xlsheet. cells [1, 3], xlsheet. cells [1, 3]). set_columnwidth (50 );

//Draw borders
Xlsheet. get_range (xlsheet. cells [1,1], Xlsheet. cells [_ row+ 2, _ Col]). Borders. set_linestyle (Microsoft. Office. InterOP. owc11.xllinestyle. xlcontinuous );

// Defines a 2-dimensional array to store data in a able.
Object [,] Dataarray = New Object [_ Row, _ Col];
// Add column name
For ( Int K = 0 ; K < _ DT. Columns. Count; k ++ )
{
Xlsheet. activesheet. cells [ 2 , K + 1 ] = _ DT. Columns [K]. Caption. tostring ();
}
// Export the data in the able to a 2-dimensional array
For ( Int I = 0 ; I < _ Row; I ++ )
{
For ( Int J = 0 ; J < _ Col; j ++ )
{
Dataarray [I, j] = _ DT. Rows [I] [J];
}
}
// Import data from a 2-dimensional array to excel
Xlsheet. get_range ( " A3 " , Xlsheet. cells [_ row + 2 , _ Col]). value2 = Dataarray;
Xlsheet. Export (_ filename, Microsoft. Office. InterOP. owc11.sheetexportactionenum. ssexportactionnone, Microsoft. Office. InterOP. owc11.sheetexportformat. ssexportxmlspreadsheet );
}
///


/// Import an Excel table
///
///
///
Public Dataset createdatasource ( String Strfilename, String Sqlstr)
{
String Strcon = " Provider = Microsoft. Jet. oledb.4.0; Data Source = " + Strfilename + " ; Extended properties = Excel 8.0; " ;
Oledbconnection olecon = New Oledbconnection (strcon );
Oledbdataadapter myda = New Oledbdataadapter (sqlstr, olecon );
Dataset myds = New Dataset ();
Myda. Fill (myds );
Return Myds;
}

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.