將DataGrid資料寫入Excel檔案

2021-04-06 23:43:48 字數 1852 閱讀 3389

程式**: 

this.enableviewstate   =   false;         

system.globalization.cultureinfo   mycitrad   =   new   system.globalization.cultureinfo("zh-cn",true);

system.io.stringwriter   ostringwriter   =   new   system.io.stringwriter(mycitrad);   

system.web.ui.htmltextwriter   ohtmltextwriter   =   new   system.web.ui.htmltextwriter(ostringwriter);

this.datagrid1.rendercontrol(ohtmltextwriter);   

response.write(ostringwriter.tostring());

response.end();

webform4.aspx----html部分:

程式**: 

<%@ page language="c#" codebehind="webform4.aspx.cs" autoeventwireup="false" inherits="

webui.webform4" %>

頁面上就乙個datagrid和乙個按鈕,按鈕用來觸發將資料到excel,後台的其他**也就不弄上來了,下面是那個關鍵的按鈕事件

程式**: 

private void button1_click(object sender, system.eventargs e)

//繫結資料到datagrid1

this.datagrid1.datasource = sourcetb.defaultview;

this.datagrid1.databind();

//將datagrid1構成的html**寫進stringwriter

this.datagrid1.page.enableviewstate = false;

system.io.stringwriter  tw   = new system.io.stringwriter();

system.web.ui.htmltextwriter hw  = new system.web.ui.htmltextwriter(tw);

this.datagrid1.rendercontrol(hw);

string htmlinfo = tw.tostring().trim();

string docfilename = "審批資訊.xls";

string filepathname = request.physicalpath;

filepathname  = filepathname.substring(0,filepathname.lastindexof("//"));

//得到excel檔案的實體地址

filepathname  = filepathname +"//" + docfilename;

system.io.file.delete(filepathname); 

filestream fs  = new filestream(filepathname, filemode.create);

binarywriter bwriter= new binarywriter(fs,system.text.encoding.getencoding("gb18030"));

//將datagrid的資訊寫入excel檔案

bwriter.write(htmlinfo);

bwriter.close();

fs.close(); }

將DataGrid中的資料完全匯入到EXCEL中

今天晚上開始測試,以前做過一次,但是很不完美。大致 如下 匯入excel中,button的click事件 private void excel btn click object sender,system.eventargs e 如果有格式轉化問題,在itemdatabound事件中寫 if e.i...

寫入資料java將資料寫入到csv檔案

這兩天一直在查詢寫入資料之類的問題,下午正好有機會和大家討論一下.對於csv件文,因為要將資料分離的寫入到個一每單元格中。用什麼來開分以後單元格和下乙個單元格之間呢?這裡就用到了 逗號可以割分單元格。舉例說明 我們這裡用乙個輸出流將資料寫入到csv件文中 fileoutputstream fos n...

將hdfs資料寫入hive

下面來嘮嘮怎麼將hdfs裡的資料寫入到hive裡。要將資料寫入hive裡,那麼hive裡得有存放資料表得位置,因此,第一步,是在hive裡建立新的表來儲存來自hdfs的資料,這裡有兩個注意 1 新建的表應該跟hdfs裡面資料表一致,指定列名 2 建立 式應一致,具體就是指row format del...