Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将多个GridView导出到excel客户端。_C#_Javascript_Asp.net_Excel_Gridview - Fatal编程技术网

C# 将多个GridView导出到excel客户端。

C# 将多个GridView导出到excel客户端。,c#,javascript,asp.net,excel,gridview,C#,Javascript,Asp.net,Excel,Gridview,我想使用和导出按钮从我的网页导出数据。数据显示在3个不同的网格视图中,我希望将其分为3个不同的工作表“top 1、top 2、top 3”。我希望使用c#和asp.net实现客户端im。。我该怎么做?我可以使用java脚本吗 这是我的密码 using System; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; usi

我想使用和导出按钮从我的网页导出数据。数据显示在3个不同的网格视图中,我希望将其分为3个不同的工作表“top 1、top 2、top 3”。我希望使用c#和asp.net实现客户端im。。我该怎么做?我可以使用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)
{

}


}
我建议使用这个组件,它是免费的,并且易于使用

用法:

    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();
    }

这样行吗?有人能在我的代码中实现吗?该解决方案不是客户端,需要在服务器上安装excel。Excel不能在服务器上或批量使用。你必须使用第三方的选项,除非你想推出你自己的,我会继续下去,并猜测,不会让你走远。你有什么建议,使这容易工作?我应该将代码更改为clientside吗?我试过epplus,但我不明白它是如何工作的。如果您不介意获取xlsx(excel 2007+)文件,我建议您使用epplus。。否则,我喜欢下面建议的speti43。好吧,epplus似乎是最好的方式。但我仍然不知道如何将代码实现到我的代码中,以使其工作。我宁愿不使用第三方