在按鈕的click事件中輸入如下代碼,即可以實現寫入excel文件。
寫excel文件時,還要把項目文件夾的權限進行設置,對iuser_machine用戶有可寫的權限。
private void button1_click(object sender, system.eventargs e)
{string filename="";
excel.applicationclass oexcel;
oexcel = new excel.applicationclass();
oexcel.usercontrol = false;
excel.workbookclass wb = (excel.workbookclass) oexcel.workbooks.add(system.reflection.missing.value);
for(int i = 1;i <= 5; i++)
{
oexcel.cells[i,1]=i.tostring();
oexcel.cells[i,2]="'第2列";
oexcel.cells[i,3]="'第3列";
oexcel.cells[i,4]="'第4列";
}
wb.saved = true;
filename= request.physicalapplicationpath + "test.xls";
oexcel.activeworkbook.savecopyas(filename);
oexcel.quit();
system.gc.collect();
response.redirect( request.applicationpath + "/test.xls");
}
新聞熱點
疑難解答
圖片精選