C # basics: C # export Excel

Source: Internet
Author: User

C # the main method for exporting Excel is as follows:
Code:
Public void CellToCell (DataSet ds)
{
# Region required objects for instantiation
// Instantiate an Excel Document Object
Microsoft. Office. Interop. Excel. Application exapp = new Microsoft. Office. Interop. Excel. Application ();
// Set visible
// If the value is false, the Excel document cannot be viewed.
Exapp. Visible = true;
// Set the workbook format
Microsoft. Office. Interop. Excel. Workbook myworkbook = exapp. Workbooks. Add (Microsoft. Office. Interop. Excel. XlWBATemplate. xlWBATWorksheet );
// Instantiate a worksheet set
Microsoft. Office. Interop. Excel. Sheets mysheets = myworkbook. Worksheets;
// Instantiate a worksheet
Microsoft. Office. Interop. Excel. Worksheet mysheet = (Microsoft. Office. Interop. Excel. Worksheet) mysheets. get_Item (1 );
# Endregion

# Region operations
For (int I = 0; I <ds. Tables [0]. Rows. Count; I ++)
{
For (int j = 0; j <ds. Tables [0]. Columns. Count; j ++)
{
Console. Write ("\ t {0} \ t |", j );
Mysheet. Cells [I + 1, j + 1] = ds. Tables [0]. Rows [I] [j]. ToString ();
}
Console. WriteLine ();
}
# Endregion
MessageBox. Show ("exported. Please do not forget to save the exported file! ");
Exapp. Caption = "Excel Demo test file ";
}
 
 
Note:
1. Match the referenced DLL version with the Office version installed on the client
2. "exception from HRESULT: 0x800A03EC" may be caused by the cell index problem. The first cell in Excel is instead.

Source code download: http://www.teamhost.org/projects/csdemo

From junzhi ridge peak
 

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.