C # create (export from database) an Excel file (including InterOP. Excel. dll)

Source: Internet
Author: User
// Create an Excel application
Excel. Application xls_exp = NULL;
Int rowindex = 1;
Int colindex = 0;
// Create a workbook and a worksheet
Excel. _ workbook xls_book = NULL;
Excel. _ worksheet xls_sheet = NULL;
Try
{
Xls_exp = new excel. applicationclass ();
Xls_book = xls_exp.workbooks.add (true );
Xls_sheet = (Excel. _ worksheet) xls_book.activesheet;
// Obtain data
Datatable AA = getdata ();
// Assign the column name of the obtained table to the cell
Foreach (datacolumn Col in AA. columns)
{
Colindex ++;
Xls_exp.cells [1, colindex] = col. columnname;
// Horizontal Alignment
Xls_sheet.get_range (xls_exp.cells [1, colindex], xls_exp.cells [1, colindex]). horizontalalignment = excel. xlvalign. xlvaligncenter;
// Vertical Alignment
Xls_sheet.get_range (xls_exp.cells [1, colindex], xls_exp.cells [1, colindex]). verticalignment = excel. xlvalign. xlvaligncenter;
// Adaptive Row Height and column width
// Xls_sheet.cells.rows.autofill ();
// Xls_sheet.cells.columns.autofill ();
}
// Process data in the same way
Foreach (datarow row in AA. Rows)
{
Rowindex ++;
Colindex = 0;
Foreach (datacolumn Col in AA. columns)
{
Colindex ++;
Switch (row [col. columnname]. GetType (). tostring ())
{
// Character
Case ("system. String "):
// Set the number format to text
Xls_sheet.get_range (xls_exp.cells [rowindex, colindex], xls_exp.cells [rowindex, colindex]). numberformatlocal = "@";
// Horizontal Alignment
Xls_sheet.get_range (xls_exp.cells [rowindex, colindex], xls_exp.cells [rowindex, colindex]). horizontalalignment = excel. xlvalign. xlvaligncenter;
// Vertical Alignment
Xls_sheet.get_range (xls_exp.cells [rowindex, colindex], xls_exp.cells [rowindex, colindex]). verticalignment = excel. xlvalign. xlvaligncenter;
Break;
// Date
Case ("system. datetime "):
// Set the number format to yyyy-mm-dd hh: mm: SS date
Xls_sheet.get_range (xls_exp.cells [rowindex, colindex], xls_exp.cells [rowindex, colindex]). numberformatlocal = "YYYY-MM-DD hh: mm: SS ";
Break;
}
// Assign values to cells
Xls_exp.cells [rowindex, colindex] = row [col. columnname];

}
}
// Invisible, that is, background processing
Xls_exp.visible = true;
Catch (exception ERR)
{
MessageBox. Show (ERR. Message );
}
// Finally
//{
// Xls_exp.quit ();
//}

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.