匯出到Excel的操作

iDotNetSpace發表於2009-11-06
 1
 2        DataTable dt = wad.GetToExcel(lblcode.Text);
 3        StringWriter sw = new StringWriter();
 4        
 5        sw.WriteLine("代號,貨品編碼,貨品名稱,單位,淨需求量,採購量,供應商,下單日期,到貨日期,採購員,報關,狀態,採購訂單號");
 6        foreach (DataRow item in dt.Rows)
 7匯出到Excel的操作        
 8            //string state = (OPSState)Convert.ToInt32(item["imgstate"]);
 9            sw.WriteLine(item["code"+ "," + item["goodscode"+ "," +
10                item["goodsname"+ "," + item["unit"+ "," +
11                item["netdemand"+ "," + item["shoppingnum"+ "," +
12                item["tradername"+ "," + item["orderdate"+ "," +
13                item["arrivaldate"+ "," + item["buyername"+ "," +
14                item["ifcustoms"+ "," + (OPSState)Convert.ToInt32(item["imgstate"]) + "," +
15                item["ordercode"] );
16        }

17        Response.AddHeader("Content-Disposition""attachment;   filename=test.csv");
18        Response.ContentType = "application/ms-excel";
19        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
20        Response.Write(sw);
21        sw.Close();
22        Response.End();

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-618271/,如需轉載,請註明出處,否則將追究法律責任。

相關文章