.NET封裝了Excel相關的類,以下是實現在程序中標工具條中點擊“EXCEL輸出”按鈕而觸發的事件,前提是你的系統中裝有EXCEL。
這是我的代碼,有注釋說明:
PRivate void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{//工具條各個按紐單擊事件
if(e.Button==excelOut)
{
Excel.application excelKccx = new Excel.Application();//創建excel對象
excelKccx.Workbooks.Add(true);//創建excel工作薄
DataTable myDataTable=myDataSet.Tables["庫存信息"];//創建一個數據表,得到DataSet中“庫存信息”表中的數據
int row=2;
//把數據表的各個信息輸入到excel表中
for(int i=0;i<myDataTable.Columns.Count;i++)//取字段名
{
excelKccx.Cells[1,i+1]=myDataTable.Columns[i].ColumnName.ToString();
}
for(int i=0;i<myDataTable.Rows.Count;i++ )//取記錄值
{
for(int j=0;j<myDataTable.Columns.Count;j++)
{
excelKccx.Cells[row,j+1]=myDataTable.Rows[i][j].ToString();
}
row++;
}
excelKccx.Visible=true;//使excel可見*/
}
else if(e.Button==reportForm)
{
//kcRptForm myReport=new kcRptForm();
//myReport.Mdiparent=this;
//myReport.Show();
}
出處:隨心所動 BLOG
新聞熱點
疑難解答