首页 > 代码库 > DataTable导出到Excel
DataTable导出到Excel
static DataTable GetTable() { // // Here we create a DataTable with four columns. // DataTable table = new DataTable(); table.Columns.Add("Dosage", typeof(int)); table.Columns.Add("Drug", typeof(string)); table.Columns.Add("Patient", typeof(string)); table.Columns.Add("Date", typeof(DateTime)); // // Here we add five DataRows. // table.Rows.Add(25, "Indocin", "David", DateTime.Now); table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now); table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now); table.Rows.Add(21, "Combivent", "Janet", DateTime.Now); table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now); return table; } private void ExporttoExcel(DataTable table) { HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.ClearHeaders(); HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.ContentType = "application/ms-excel"; HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">"); HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Reports.xls"); HttpContext.Current.Response.Charset = "utf-8"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250"); //sets font HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>"); HttpContext.Current.Response.Write("<BR><BR><BR>"); //sets the table border, cell spacing, border color, font of the text, background, foreground, font height HttpContext.Current.Response.Write("<Table border='1' bgColor='#ffffff' " + "borderColor='#000000' cellSpacing='0' cellPadding='0' " + "style='font-size:10.0pt; font-family:Calibri; background:white;'> <TR>"); //am getting my grid's column headers //int columnscount = GridView_Result.Columns.Count; for (int j = 0; j < table.Columns.Count; j++) { //write in new column HttpContext.Current.Response.Write("<Td>"); //Get column headers and make it as bold in excel columns HttpContext.Current.Response.Write("<B>"); HttpContext.Current.Response.Write(table.Columns[j].ColumnName.ToString()); HttpContext.Current.Response.Write("</B>"); HttpContext.Current.Response.Write("</Td>"); } HttpContext.Current.Response.Write("</TR>"); foreach (DataRow row in table.Rows) {//write in new row HttpContext.Current.Response.Write("<TR>"); for (int i = 0; i < table.Columns.Count; i++) { HttpContext.Current.Response.Write("<Td>"); HttpContext.Current.Response.Write(row[i].ToString()); HttpContext.Current.Response.Write("</Td>"); } HttpContext.Current.Response.Write("</TR>"); } //if (table != null) //{ // foreach (DataColumn dc in table.Columns) // { // Response.Write(dc.ColumnName + "\t"); // } // Response.Write(System.Environment.NewLine); // foreach (DataRow dr in table.Rows) // { // for (int i = 0; i < table.Columns.Count; i++) // { // Response.Write(dr[i].ToString() + "\t"); // } // } // // range.Columns.AutoFit(); // //excel.Visible = true; // //Microsoft.Office.Interop.Excel.Range xlRange = null; // //xlRange = worksheet.get_Range("A1", "T1"); // //xlRange.Columns.AutoFit(); // //Response.Write("\n"); // //Response.End(); //} HttpContext.Current.Response.Write("</Table>"); HttpContext.Current.Response.Write("</font>"); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End(); }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。