(From http://www.cnblogs.com/osamede/archive/2008/04/14/1153414.html)
, Generate Excel , Although you can Pair Excel File Proceed Full control, It can generate any complicated format, but it has a major disadvantage. This method will generate many Excel Process , Difficult to completely clear Division Especially when an error occurs, The entire server may crash. This article introduces C # Write open-source components. Office2003 And XML Format.
1 operation Excel binary format
OpenOffice.org two published documents Excel File Format (biff8) specification and Microsoft compound document (ole2) format specification pairs Excel provides a detailed description of the binary format, you can directly Office binary format document.
MyxlsIs an open-source component written in C #. It can be used to generate EXCEL files with many tables and in the inclusion format. It provides an object-based API that is very easy to use.
1To generate an empty table.
1 Xlsdocument xls = New Xlsdocument (); // Create an empty Excel document
2
3 XLS. Send (); // Send the document to the browser.
2. Create a complex vertex table Xlsdocument xls = New Xlsdocument ();
XLS. filename = " Wacky.xls " ;
// Add file attributes
XLS. summaryinformation. Author = " Tim Erickson " ; // Author
XLS. summaryinformation. Subject = " A wacky display of Excel file generation " ;
XLS. documentsummaryinformation. Company = " In2bits.org " ;
For ( Int Sheetnumber = 1 ; Sheetnumber <= 5 ; Sheetnumber ++ )
{
String Sheetname = " Sheet " + Sheetnumber;
Int Rowmin = Sheetnumber;
Int Rowcount = Sheetnumber + 10 ;
Int Colmin = Sheetnumber;
Int Colcount = Sheetnumber + 10 ;
// Create 5 tables
Worksheet Sheet = XLS. Workbook. worksheets. addnamed (sheetname );
Cells = Sheet. cells;
For ( Int R = 0 ; R < Rowcount; R ++ )
{
If (R = 0 )
{
For ( Int C = 0 ; C < Colcount; c ++ )
{
// Create colcount cells in a row
Cells. Add (rowmin + R, colmin + C, " Bytes " + (C + 1 ). Font. Bold = True ;
}
}
Else
{
For ( Int C = 0 ; C < Colcount; c ++ )
{
Int Val = R + C;
Cell cell = Cells. Add (rowmin + R, colmin + C, Val );
If (Val % 2 ! = 0 )
{
Cell. Font. fontname = " Times New Roman " ;
Cell. Font. Underline = Underlinetypes. Double; // Add a Double underline to the bottom of the text
Cell. Rotation = 45 ; // Rotate cell text 45 degrees
}
}
}
}
}
An Excel file contains five tables.
Binary XML format
Office2003 or later versions support the XML format, so that the content can be directly converted into XML that can be recognized by the Office through some templates, limited by time, and will be discussed with you next time :-)