.NET封裝了Excel相關(guān)的類(lèi),以下是實(shí)現(xiàn)在程序中標(biāo)工具條中點(diǎn)擊“EXCEL輸出”按鈕而觸發(fā)的事件,前提是你的系統(tǒng)中裝有EXCEL。
這是我的代碼,有注釋說(shuō)明:
PRivate void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{//工具條各個(gè)按紐單擊事件
if(e.Button==excelOut)
{
Excel.application excelKccx = new Excel.Application();//創(chuàng)建excel對(duì)象
excelKccx.Workbooks.Add(true);//創(chuàng)建excel工作薄
DataTable myDataTable=myDataSet.Tables["庫(kù)存信息"];//創(chuàng)建一個(gè)數(shù)據(jù)表,得到DataSet中“庫(kù)存信息”表中的數(shù)據(jù)
int row=2;
//把數(shù)據(jù)表的各個(gè)信息輸入到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可見(jiàn)*/
}
else if(e.Button==reportForm)
{
//kcRptForm myReport=new kcRptForm();
//myReport.Mdiparent=this;
//myReport.Show();
}
出處:隨心所動(dòng) BLOG
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注