2012-07-10 17 views
0

我想使用导出按钮从我的网页导出数据。数据显示在3个不同的GridView中,我想分离到3个不同的工作表“顶部1,顶部2,顶部3”我想用c#和asp.net做这个客户端。我该怎么做?我可以使用Java脚本吗?导出多个gridviews以优化客户端。

这里是我的代码

using System; 
using System.Configuration; 
using System.Linq; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Xml.Linq; 
using System.Data; 
using System.Data.SqlClient; 
using System.Collections; 
using System.Text; 
using System.IO; 
using Excel = Microsoft.Office.Interop.Excel; 
using System.Reflection; 



public partial class _Default : System.Web.UI.Page 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 
    DataSet dataSet = new DataSet(); 

    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ISALog1ConnectionString"].ToString()); 

    SqlCommand cmd = new SqlCommand("exec ProxyReport", conn); 
    cmd.CommandTimeout = 200; 

    SqlDataAdapter ad = new SqlDataAdapter(cmd); 
    ad.Fill(dataSet); 

    GridView1.DataSource = dataSet.Tables[0]; 
    GridView1.DataBind(); 
    GridView2.DataSource = dataSet.Tables[1]; 
    GridView2.DataBind(); 
    GridView3.DataSource = dataSet.Tables[2]; 
    GridView3.DataBind(); 

} 
protected void Button1_Click(object sender, EventArgs e) 
{ 
    string attachment = "attachment; filename=Top 1.xls"; 
    Response.ClearContent(); 
    Response.AddHeader("content-disposition", attachment); 
    Response.Cache.SetCacheability(HttpCacheability.NoCache); 
    Response.ContentType = "application/ms-excel"; 
    StringWriter sw = new StringWriter(); 
    HtmlTextWriter htw = new HtmlTextWriter(sw); 
    GridView1.RenderControl(htw); 
    GridView2.RenderControl(htw); 
    GridView3.RenderControl(htw); 
    Response.Write(sw.ToString()); 
    Response.End(); 
} 
protected void GetExcel(object sender, EventArgs e) 
{ 
    //3 gridviews each in 3 different worksheets 
} 

public override void VerifyRenderingInServerForm(Control control) 
{ 

} 


} 
+0

将这项工作?有人可以将其实施到我的代码中吗? http://stackoverflow.com/questions/7632782/export-gridview-to-multiple-excel-sheet – Cloud 2012-07-10 15:07:32

+0

该解决方案不是客户端,并需要在服务器上的excel。 Excel并不意味着在服务器上或批量使用。您必须使用第三方选项,除非您想要推出自己的产品,而我将继续推测,并不会让您走得更远。 – banging 2012-07-10 16:14:35

+0

对于此功能,您会有什么建议可轻松使用?我应该将代码更改为客户端吗?我试过epplus,但我不明白这是如何工作的。 – Cloud 2012-07-10 16:19:47

回答

1

我建议使用NPOI组件对于这一点,它的自由,和易于使用。

用法:

public static void GetExcel (DataTable sourceTable1,DataTable sourceTable2, DataTable sourceTable3, string fileName) 
    { 
     HSSFWorkbook workbook = new HSSFWorkbook(); 
     MemoryStream memoryStream = new MemoryStream(); 
     HSSFSheet sheet = workbook.CreateSheet("Sheet1") as HSSFSheet; 
     HSSFSheet sheet2 = workbook.CreateSheet("Sheet2") as HSSFSheet; 
     HSSFSheet sheet3 = workbook.CreateSheet("Sheet3") as HSSFSheet; 
     HSSFRow headerRow = sheet.CreateRow(0) as HSSFRow; 

     sheet.SetColumnWidth(0, 7000); 
     sheet.SetColumnWidth(1, 7000); 
     sheet.SetColumnWidth(2, 7000); 
     sheet.SetColumnWidth(3, 10000); 
     sheet.SetColumnWidth(4, 7000); 

     sheet.DefaultRowHeight = 3000; 


     HSSFPalette palette = workbook.GetCustomPalette(); 
     palette.SetColorAtIndex(HSSFColor.BLUE.index, 41, 113, 153); 
     palette.SetColorAtIndex(HSSFColor.GREEN.index, 129, 179, 78); 
     palette.SetColorAtIndex(HSSFColor.CORAL.index, 235, 235, 235); 
     palette.SetColorAtIndex(HSSFColor.LIGHT_BLUE.index, 233, 241, 245); 

     IFont headerFont = workbook.CreateFont(); 
     headerFont.FontHeightInPoints = 16; 
     headerFont.FontName = "Palatino Linotype"; 
     headerFont.Boldweight = 5; 
     headerFont.Color = HSSFColor.WHITE.index; 

     IFont font = workbook.CreateFont(); 
     font.FontHeightInPoints = 14; 
     font.FontName = "Palatino Linotype"; 
     font.Boldweight = 5; 
     font.Color = HSSFColor.BLUE.index; 

     HSSFCellStyle headerStyle = (HSSFCellStyle)workbook.CreateCellStyle(); 
     headerStyle.FillForegroundColor = HSSFColor.GREEN.index; ; 
     headerStyle.FillPattern = NPOI.SS.UserModel.FillPatternType.SOLID_FOREGROUND; 
     headerStyle.BorderBottom = BorderStyle.THIN; 
     headerStyle.BottomBorderColor = HSSFColor.WHITE.index; 
     headerStyle.BorderRight = BorderStyle.THIN; 
     headerStyle.RightBorderColor = HSSFColor.WHITE.index; 
     headerStyle.BorderLeft = BorderStyle.THIN; 
     headerStyle.LeftBorderColor = HSSFColor.WHITE.index; 
     headerStyle.BorderTop = BorderStyle.THIN; 
     headerStyle.TopBorderColor = HSSFColor.WHITE.index; 
     headerStyle.VerticalAlignment = VerticalAlignment.CENTER; 
     headerStyle.Alignment = HorizontalAlignment.CENTER; 
     headerStyle.SetFont(headerFont); 

     HSSFCellStyle oddStyle = (HSSFCellStyle)workbook.CreateCellStyle(); 
     oddStyle.FillForegroundColor = HSSFColor.CORAL.index; 
     oddStyle.FillPattern = NPOI.SS.UserModel.FillPatternType.SOLID_FOREGROUND; 
     oddStyle.BorderBottom = BorderStyle.THIN; 
     oddStyle.BottomBorderColor = HSSFColor.WHITE.index; 
     oddStyle.BorderRight = BorderStyle.THIN; 
     oddStyle.RightBorderColor = HSSFColor.WHITE.index; 
     oddStyle.BorderLeft = BorderStyle.THIN; 
     oddStyle.LeftBorderColor = HSSFColor.WHITE.index; 
     oddStyle.BorderTop = BorderStyle.THIN; 
     oddStyle.TopBorderColor = HSSFColor.WHITE.index; 
     oddStyle.VerticalAlignment = VerticalAlignment.CENTER; 
     oddStyle.Alignment = HorizontalAlignment.CENTER; 
     oddStyle.SetFont(font); 

     HSSFCellStyle evenStyle = (HSSFCellStyle)workbook.CreateCellStyle(); 
     evenStyle.FillForegroundColor = HSSFColor.LIGHT_BLUE.index; 
     evenStyle.FillPattern = NPOI.SS.UserModel.FillPatternType.SOLID_FOREGROUND; 
     evenStyle.BorderBottom = BorderStyle.THIN; 
     evenStyle.BottomBorderColor = HSSFColor.WHITE.index; 
     evenStyle.BorderRight = BorderStyle.THIN; 
     evenStyle.RightBorderColor = HSSFColor.WHITE.index; 
     evenStyle.BorderLeft = BorderStyle.THIN; 
     evenStyle.LeftBorderColor = HSSFColor.WHITE.index; 
     evenStyle.BorderTop = BorderStyle.THIN; 
     evenStyle.TopBorderColor = HSSFColor.WHITE.index; 
     evenStyle.VerticalAlignment = VerticalAlignment.CENTER; 
     evenStyle.Alignment = HorizontalAlignment.CENTER; 
     evenStyle.SetFont(font); 

     // handling header. 
     foreach (DataColumn column in sourceTable.Columns) 
     { 
      var headercell = headerRow.CreateCell(column.Ordinal); 
      headercell.SetCellValue(column.ColumnName); 
      headercell.CellStyle = headerStyle; 
      headerRow.Height = 600; 
     } 

     // handling value. 
     int rowIndex = 1; 

     //Create 3 sheet, and fill them 
     foreach (DataRow row in sourceTable.Rows) 
     { 
      HSSFRow dataRow = sheet.CreateRow(rowIndex) as HSSFRow; 
      dataRow.Height = 600; 
      foreach (DataColumn column in sourceTable.Columns) 
      { 
       var cell = dataRow.CreateCell(column.Ordinal); 
       cell.SetCellValue(row[column].ToString()); 

       if (sourceTable.Rows.IndexOf(row) % 2 == 0) 
       { 
        cell.CellStyle = evenStyle; 
       } 
       else 
       { 
        cell.CellStyle = oddStyle; 
       } 
      } 

      rowIndex++; 
     } 

     workbook.Write(memoryStream); 
     memoryStream.Flush(); 

     HttpResponse response = HttpContext.Current.Response; 
     response.ContentType = "application/vnd.ms-excel"; 
     response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", fileName)); 
     response.Clear(); 

     response.BinaryWrite(memoryStream.GetBuffer()); 
     response.End(); 
    } 
+0

我不想使用第三方 – Cloud 2012-07-10 15:46:17