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:
- Match the referenced DLL version with the Office version installed on the client
- "Exception from HRESULT: 0x800A03EC" may be caused by the cell index problem. The first cell in Excel is instead.
Reference website
- Http://support.microsoft.com/kb/306023/zh-cn
- Http://www.cnblogs.com/jinliangliu/archive/2006/08/18/480391.html
- Http://www.cnblogs.com/mr_ke/archive/2010/03/02/1676210.html
- Http://www.cnblogs.com/dahuzizyd/category/64340.html