【问题标题】:Couldn't export excel file through asp.net c#无法通过asp.net c#导出excel文件
【发布时间】:2016-02-23 18:52:18
【问题描述】:

我正在使用此代码将 GridView 数据导出到 excel,代码工作正常,但 Excel 文件不支持某些操作;如果我正在从此文件中打印标签,则会显示“错误:外部表不是预期格式” 该怎么办??? 有什么方法可以简单或不使用任何格式导出 Excel,因为 excel 的格式与 GridView 相同...

protected void btnExcel1_Click(object sender, ImageClickEventArgs e)
{
    Response.ClearContent();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "ExcelSheet.xls"));
    Response.ContentType = "application/ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    GridView1.AllowPaging = false;
    //Change the Header Row back to white color
    GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
    //Applying stlye to gridview header cells
    for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
    {
        GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
    }
    int j = 1;
    //This loop is used to apply stlye to cells based on particular row
    foreach (GridViewRow gvrow in GridView1.Rows)
    {
        gvrow.BackColor = Color.White;
        if (j <= GridView1.Rows.Count)
        {
            if (j % 2 != 0)
            {
                for (int k = 0; k < gvrow.Cells.Count; k++)
                {
                    gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
                }
            }
        }
        j++;
    }
    GridView1.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.End();
}

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    试试下面的代码:-

    using Excel = Microsoft.Office.Interop.Excel;
    using System.Reflection;
    using System.IO;
    
            //Print using Ofice InterOp
            Excel.Application excel = new Excel.Application();
    
            var workbook = (Excel._Workbook)(excel.Workbooks.Add(Missing.Value));
    
            for (var i = 0; i < dataset.Tables.Count; i++)
            {
    
                    if (workbook.Sheets.Count <= i)
                    {
                        workbook.Sheets.Add(Type.Missing, Type.Missing, Type.Missing,
                                            Type.Missing);
                    }
    
                    //NOTE: Excel numbering goes from 1 to n
                    var currentSheet = (Excel._Worksheet)workbook.Sheets[i + 1]; 
    
                    for (var y = 0; y < dataset.Tables[i].Rows.Count; y++)
                    {
                        for (var x = 0; x < dataset.Tables[i].Rows[y].ItemArray.Count(); x++)
                        {
                            currentSheet.Cells[y+1, x+1] = dataset.Tables[i].Rows[y].ItemArray[x];
                        }
                    }
            }
    
            string outfile = @"C:\excel.xlsx";
    
            workbook.SaveAs( outfile, Type.Missing, Type.Missing, Type.Missing,
                            Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
                            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                            Type.Missing);
    
            workbook.Close();
            excel.Quit();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-24
      相关资源
      最近更新 更多