.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
新聞熱點
疑難解答
圖片精選