-1

我最近从 Visual Studio 2005 开始在Visual Studio 2010中编写代码。我需要代码才能从数据网格导出到Excel 。在 Visual Studio 2005 中,使用了以下代码。

    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;filename=dgd.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.xls";
    System.IO.StringWriter stringWrite = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
    dgd.Visible = true;
    dgd.RenderControl(htmlWrite);
    Response.Write(stringWrite.ToString());
    Response.End();

这在 Visual Studio 2005 中不会产生相同的结果。标题未与列对齐。在 Excel 中未获取 datagrid 中的图片,并且 datagrid 中的链接无法正确显示。什么是更好的代码?

4

4 回答 4

0

下面的代码可以帮助您使用 gridview 编写 Excel 工作表。

// Function to export datagridview to excel sheet
// excel_file contains the path to the excel file.
public void export_to_excel(DataGridView dgv, string excel_file)
{
    int cols;

    //Open file
    StreamWriter wr = new StreamWriter(excel_file);

    //Determine the number of columns and write columns to file
    cols = dgv.Columns.Count;
    for (int i = 0; i < cols; i++)
    {
        wr.Write(dgv.Columns[i].HeaderText.ToString().ToUpper() + "\t");
    }
    wr.WriteLine();

    //Write rows to the Excel file
    for (int i = 0; i < (dgv.Rows.Count - 1); i++)
    {
        for (int j = 0; j < cols; j++)
        {
            if (dgv.Rows[i].Cells[j].Value != null)
                wr.Write(dgv.Rows[i].Cells[j].Value + "\t");
            else
            {
                wr.Write("\t");
            }
        }
        wr.WriteLine();
    }

    //Close file
    wr.Close();
}

此外,您还可以阅读博客文章如何将数据从 ASP.NET 应用程序导出到 Excel + 避免文件格式不同提示

于 2013-06-13T04:58:41.327 回答
0

我在我们的项目中使用相同的。

 private void ExportToExcel(DataTable dt)
    {
        if (dt.Rows.Count > 0)
        {
            string filename = "DownloadReport.xls";
            System.IO.StringWriter tw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
            DataGrid dgGrid = new DataGrid();
            dgGrid.DataSource = dt;
            dgGrid.DataBind();

            //Get the HTML for the control.
            dgGrid.RenderControl(hw);
            //Write the HTML back to the browser.
            Response.ContentType = "application/vnd.ms-excel";
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
            this.EnableViewState = false;
            Response.Write(tw.ToString());
            Response.End();
        }
    }

希望对你有帮助

于 2013-06-13T04:59:15.297 回答
0
Add following code after htmlwriter line


`if (dtDetails.Rows.Count > 0)
            {
                for (int i = 0; i < gvProduction.HeaderRow.Cells.Count; i++)
                {
                    gvProduction.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 gvProduction.Rows)
                {
                    gvrow.BackColor = Color.White;
                    if (j <= gvProduction.Rows.Count)
                    {
                        if (j % 2 != 0)
                        {
                            for (int k = 0; k < gvrow.Cells.Count; k++)
                            {
                                gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
                            }
                        }
                    }
                    j++;
                }
                gvProduction.RenderControl(hw);
                Response.Write(sw.ToString());
                Response.End();
            }`
于 2013-06-13T04:55:50.327 回答
0

您可以使用以下代码。

在导出按钮上单击:

FileInfo FI = new FileInfo(Path);
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
DataGrid DataGrd = new DataGrid();
DataGrd.DataSource = dt1;
DataGrd.DataBind();

DataGrd.RenderControl(htmlWrite);
string directory = Path.Substring(0, Path.LastIndexOf("\\")); // GetDirectory(Path);
if (!Directory.Exists(directory))
{
    Directory.CreateDirectory(directory);
}

System.IO.StreamWriter vw = new System.IO.StreamWriter(Path, true);
stringWriter.ToString().Normalize();
vw.Write(stringWriter.ToString());
vw.Flush();
vw.Close();
WriteAttachment(FI.Name, "application/vnd.ms-excel", stringWriter.ToString());

附件编写代码:

public static void WriteAttachment(string FileName, string FileType, string content)
{
   HttpResponse Response = System.Web.HttpContext.Current.Response;
   Response.ClearHeaders();
   Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
   Response.ContentType = FileType;
   Response.Write(content);
   Response.End();
}

有关更多参考,请参阅在 ASP.NET 中将 Gridview 数据导出到 Excel

于 2013-06-13T04:57:15.700 回答