Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.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# 直接导出到excel到.xls而不是html_C#_Asp.net_Excel_Gridview - Fatal编程技术网

C# 直接导出到excel到.xls而不是html

C# 直接导出到excel到.xls而不是html,c#,asp.net,excel,gridview,C#,Asp.net,Excel,Gridview,下面是导出到excel my GridView但格式化HTML的代码。导出后,我需要将此文件导入仅接受原始.xls文件的系统。这是将gridview直接保存为原始.xls格式的方法吗 protected void Btn_Excel_Click(object sender, EventArgs e) { string kodowanie = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf

下面是导出到excel my GridView但格式化HTML的代码。导出后,我需要将此文件导入仅接受原始.xls文件的系统。这是将gridview直接保存为原始.xls格式的方法吗

protected void Btn_Excel_Click(object sender, EventArgs e)
{
        string kodowanie = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
        Response.ContentEncoding = System.Text.Encoding.UTF8;//aby widział polskie znaki
        Response.Clear();
        Response.Buffer = true;
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "ImportWZ" + DateTime.Now.ToString() + ".xls"));
        Response.Charset = "";
        this.EnableViewState = false;
        System.IO.StringWriter sw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);


        if (ViewState["CheckBoxArray"] != null)
        {
            ArrayList CheckBoxArray = (ArrayList)ViewState["CheckBoxArray"];
            string checkAllIndex = "chkAll-" + GridView1.PageIndex;
            int rowIdx = 0;
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                GridViewRow row = GridView1.Rows[i];
                row.Visible = false;

                if (row.RowType == DataControlRowType.DataRow)
                {
                    if (CheckBoxArray.IndexOf(i + 1) != -1)
                    {
                        row.Visible = true;
                        row.BackColor = System.Drawing.Color.White;
                        row.Cells[0].Visible = false;
                        row.Attributes.Add("class", "textmode");

                        rowIdx++;
                    }
                }
            }
        }

        GridView1.HeaderRow.Style.Add("background-color", "#003389");
        GridView1.HeaderRow.Style.Add("color", "white");
        GridView1.RenderControl(htw);
        Response.Write(kodowanie+sw.ToString());
        Response.End();

}
public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
{

}

您必须使用支持xls文件的Excel库,例如


如果您决定使用EasyXLS,请查看他们的代码示例。

EPPLUS可以导出到xlsx它是否接受openoffice电子表格/Excel 2007.xlsx文件?不,我要说更多。他们只接受较旧的.xls97-2003格式。你应该做的第一件事就是对他们大喊大叫,因为他们不接受.xlsx文件。告诉他们不智能,然后将他们指向使Excel 2003能够打开.xlsx文件的。如果他们绝对不接受.xlsx文件,那就再对他们大喊大叫。然后,您需要找到一个转换器程序,或者在服务器上使用Office Interop,Microsoft强烈建议不要使用,这将给您带来很多麻烦。老实说,如果我是你,我会拒绝支持.xls。另一种解决方法可能是将文件另存为HTML,但将其扩展名改为.xls,但每次打开文件时,Excel都会抱怨文件可能已损坏,但会停止打开。但是,您在如何设置样式方面非常有限。我不会通过将GridView呈现为字符串来实现这一点。相反,我会使用Razor引擎或手动构建HTML字符串,这样您就可以完全控制生成的HTML。