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