我想使用和导出按钮从我的网页导出数据。数据显示在3个不同的网格视图中,我想分成3个不同的工作表“top 1,top 2,top3”我想用c#和asp.net做客户端..我怎么能这样做?我可以使用java脚本吗?
这是我的代码
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 :(得分: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();
}