excel| Data///<summary>
Import all the data from the DataSet into Excel.
You need to add the Com:microsoft Excel Object Library.
Using Excel;
</summary>
<param name= "FilePath" ></param>
<param name= "DS" ></param>
public static void exportToExcel (String filePath, DataSet DS)
{
object omissing = System.Reflection.Missing.Value;
Excel.applicationclass xlapp = new Excel.applicationclass ();
Try
{
Open the Excel file. The following is Office 2000.
Excel.Workbook Xlworkbook = XlApp.Workbooks.Open (FilePath, omissing, omissing, omissing, omissing, omissing,
omissing, omissing, omissing, omissing, omissing, omissing,
omissing);
Excel.Worksheet Xlworksheet;
Loop all DataTable
for (int i=0; i<ds. Tables.count; i++)
{
Add to a new sheet page.
Xlworksheet = (excel.worksheet) xlWorkbook.Worksheets.Add (omissing,omissing,1,omissing);
Take TableName as the newly added sheet page name.
Xlworksheet.name = ds. Tables[i]. TableName;
Take out all the values in this DataTable and hold them in the StringBuffer.
String stringbuffer = "";
for (int j=0; j<ds. Tables[i]. Rows.Count; J + +)
{
for (int k=0; k<ds. Tables[i]. Columns.count; k++)
{
StringBuffer + + ds. Tables[i]. ROWS[J][K]. ToString ();
if (K < DS. Tables[i]. COLUMNS.COUNT-1)
StringBuffer + = "T";
}
StringBuffer + = "\ n";
}
Using the System shearing board
System.Windows.Forms.Clipboard.SetDataObject ("");
Place the StringBuffer into the Clipboard.
System.Windows.Forms.Clipboard.SetDataObject (StringBuffer);
Select the first cell in this sheet page
((Excel.Range) xlworksheet.cells[1,1]). Select ();
Paste!
Xlworksheet.paste (omissing,omissing);
Clear the system Clipboard.
System.Windows.Forms.Clipboard.SetDataObject ("");
}
Save and close the workbook.
Xlworkbook.close (Excel.XlSaveAction.xlSaveChanges, omissing, omissing);
System.Runtime.InteropServices.Marshal.ReleaseComObject (Xlworkbook);
Xlworkbook = null;
}
catch (Exception ex)
{
MessageBox.Show (ex. message);
}
Finally
{
Release...
xlApp.Quit ();
System.Runtime.InteropServices.Marshal.ReleaseComObject (xlapp);
xlapp = null;
Gc. Collect ();
}
}
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.