标签:
文章转自网上的一位朋友,非常感谢!
后台代码
public void ExportDataTableToExcel(System.Data.DataTable s_DataTable) { int tRowCount = s_DataTable.Rows.Count; int tColumnCount = s_DataTable.Columns.Count; Response.Expires = 0; Response.Clear(); Response.Buffer = true; Response.Charset = "utf-8"; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.ContentType = "application/vnd.ms-<a href=‘http://www.it165.net/edu/ebg/‘ target=‘_blank‘ class=‘keylink‘>excel</a>"; //设定档名可为中文_#1 Response.AddHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode("Y2J中文档名", System.Text.Encoding.UTF8) + ".xls\""); ////引用这三个xmlns //Response.Write("<html xmlns:o=‘urn:schemas-microsoft-com:office:office‘"); //Response.Write("xmlns:x=‘urn:schemas-microsoft-com:office:<a href=‘http://www.it165.net/edu/ebg/‘ target=‘_blank‘ class=‘keylink‘>excel</a>‘"); //Response.Write("xmlns=‘http://www.w3.org/TR/REC-html40‘>"); Response.Write("<meta http-equiv=Content-Type content=text/html;charset=utf-8>"); //在head中加入xml定义 Response.Write("\n <head>"); Response.Write("\n <xml>"); Response.Write("\n <x:ExcelWorkbook>"); Response.Write("\n <x:ExcelWorksheets>"); Response.Write("\n <x:ExcelWorksheet>"); //设定此Worksheet名称_#2 Response.Write("\n <x:Name>Y2J测试</x:Name>"); //以下针对此工作表进行属性设定 Response.Write("\n <x:WorksheetOptions>"); Response.Write("\n <x:FrozenNoSplit/>"); //设定冻结行号_#3 Response.Write("\n <x:SplitHorizontal>1</x:SplitHorizontal>"); //设定起始行号(TopRowBottomPane)_#4 Response.Write("\n <x:TopRowBottomPane>2</x:TopRowBottomPane>"); Response.Write("\n <x:ActivePane>2</x:ActivePane>"); Response.Write("\n </x:WorksheetOptions>"); Response.Write("\n </xml>"); Response.Write("\n </head>"); Response.Write("\n <body>"); Response.Write("<Table borderColor=black border=1>"); Response.Write("\n <TR>"); //塞入head for (int i = 0; i < tColumnCount; i++) { //设定head的背景色_#5 Response.Write("\n <TD bgcolor = #fff8dc>"); Response.Write(s_DataTable.Columns[i].ColumnName); Response.Write("\n </TD>"); } Response.Write("\n </TR>"); //塞入每一笔资料 for (int j = 0; j < tRowCount; j++) { Response.Write("\n <TR>"); for (int k = 0; k < tColumnCount; k++) { if (s_DataTable.Rows[j]["ProviderType"].ToString() == "3") { //设定单号资料靠左且背景为绿色_#6 Response.Write("\n <TD align=\"left\" x:num bgcolor = ADFF2F><font color=‘red‘>"); Response.Write(s_DataTable.Rows[j][k].ToString()); Response.Write("\n </font></TD>"); } else { Response.Write("\n <TD align=\"right\" x:num>"); Response.Write(s_DataTable.Rows[j][k].ToString()); Response.Write("\n </TD>"); } if (j % 2 == 0) { } else { //设定双号资料靠右_#7 } } Response.Write("\n </TR>"); } Response.Write("</Table>"); Response.Write("</body>"); Response.Write("</html>"); Response.End(); }
连接字符串
protected void Button1_Click(object sender, EventArgs e) { string connectionString = "Data Source=JJ-PC;Initial Catalog=xTest;Integrated Security=True;user id=sa;Password=123"; using (SqlConnection con = new SqlConnection(connectionString)) { con.Open(); //sql语句查询 string commandString = "Select * from tb_schoose"; SqlCommand cmd = new SqlCommand(commandString, con); System.Data.DataTable dt=cmd.ExecuteReader().GetSchemaTable(); ExportDataTableToExcel(dt); //TT(); } }
前台代码
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"></asp:Button>
标签:
原文地址:http://www.cnblogs.com/yuanjiehot/p/4395042.html