0

我使用以下代码下载 Excel 工作表。 在此处输入图像描述

上面的代码是我的逻辑的起始代码。下载后,我打开 Excel 表。它显示这样的警告

在此处输入图像描述

由于这个警告,当我尝试使用 Asp.Net 将同一页面上传到 Mysql 数据库时,它会显示一个异常,如“页面格式不正确”。

这是我下载 Excel 工作表的全部逻辑

protected void download_Click(object sender, EventArgs e)
 {

    ExportToExcel(SqlDataSource1, "StudentMarks");
  }
public void ExportToExcel(SqlDataSource dataSrc, string fileName)
{
    //Add Response header 
    Response.Clear();
    Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", fileName));
    Response.Charset = "";
    Response.ContentType = "application/ms-excel";
    //GET Data From Database                
    MySqlConnection cn = new MySqlConnection(dataSrc.ConnectionString);
    // string query =  dataSrc.SelectCommand.Replace("\r\n", " ").Replace("\t", " ");
    MySqlCommand cmd9 = new MySqlCommand("select subject from class_subject where standard='" + DropDownList1.SelectedItem.Value + "';", cn);
    // cn.Open();

    //DataSet ds9=new DataSet();
    MySqlDataAdapter da9 = new MySqlDataAdapter(cmd9);
    // da9.Fill(ds9);
    DataTable dt = new DataTable();
    da9.Fill(dt);

    StringBuilder sb9 = new StringBuilder();




    for (int count = 0; count < dt.Rows.Count; count++)
    {
        // string headerRowText = GridView4.HeaderRow.Cells[i].Text;
        sb9.Append("'");
        sb9.Append(dt.Rows[count][0].ToString());
        sb9.Append("'");
        if (count < dt.Rows.Count - 1)
        //if (count < count3 - 1)
        {
            sb9.Append(",");
        }
    }
    Label2.Text = sb9.ToString();
    //MySqlCommand fyearcmd = new MySqlCommand("select fyear from student_data where standard='" + DropDownList1.SelectedItem.Value + "' and completed_status='running';", cn);
    //MySqlDataReader fyeardr = fyearcmd.ExecuteReader();

    string query = "select Admission_Num,Name,'Standard','Fyear','Type_of_exam'," + sb9.ToString() + " from student_data where standard='" + DropDownList1.SelectedItem.Value + "' and fyear='" + DropDownList4.SelectedItem.Value + "';";

    MySqlCommand cmd = new MySqlCommand(query, cn);

    cmd.CommandTimeout = 999999;
    cmd.CommandType = CommandType.Text;
    try
    {
        cn.Open();
        MySqlDataReader dr = cmd.ExecuteReader();
        StringBuilder sb = new StringBuilder();
        //Session["fieldcount"] = dr.FieldCount.ToString();
        //Label1.Text = dr.FieldCount.ToString();
        //Add Header  
        int count3 = 4;
        for (int count = 0; count < dr.FieldCount; count++)
        //for (int count = 0; count < count3; count++)
        {
            if (dr.GetName(count) != null)
                sb.Append(dr.GetName(count));
            if (count < dr.FieldCount - 1)
            //if (count < count3 - 1)
            {
                sb.Append("\t");
            }
        }
        Response.Write(sb.ToString() + "\n");
        Response.Flush();
        //Append Data
        while (dr.Read())
        {
            sb = new StringBuilder();

            //for (int col = 0; col < dr.FieldCount - 1; col++)
            for (int col = 0; col <= count3; col++)
            {
                if (col < (count3 - 2))
                {

                    if (!dr.IsDBNull(col))
                        sb.Append(dr.GetValue(col).ToString().Replace(",", " "));
                    sb.Append("\t");
                }
                if (col == (count3 - 2))
                {

                    if (!dr.IsDBNull(col))
                        sb.Append(DropDownList1.SelectedItem.Text);
                    sb.Append("\t");
                }
                if (col == (count3 - 1))
                {
                    if (!dr.IsDBNull(col))
                    {
                        //sb.Append(dr.GetValue(col).ToString().Replace(",", " "));
                        sb.Append(DropDownList4.SelectedItem.Text);
                    }
                    sb.Append("\t");
                }
                if (col == count3)
                {
                    if (!dr.IsDBNull(col))
                    {
                        //sb.Append(dr.GetValue(col).ToString().Replace(",", " "));
                        sb.Append(DropDownList3.SelectedItem.Text);
                    }
                    sb.Append("\t");
                }
            }
            //if (!dr.IsDBNull(dr.FieldCount - 1))
            //    sb.Append(dr.GetValue(dr.FieldCount - 1).ToString().Replace(",", " "));
            Response.Write(sb.ToString() + "\n");
            Response.Flush();

        }
        dr.Dispose();
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }
    finally
    {
        cmd.Connection.Close();
        cn.Close();
    }
    Response.End();
}

所以请给我建议如何在打开时不显示错误的情况下下载 Excel 表。

4

1 回答 1

1

您正在生成一个 csv 文件,而不是一个 excel 文件。尝试更改标题:

Response.ContentType = "text/csv";
Response.AddHeader("Content-Disposition", "attachment;filename=myfilename.csv");

通常 excel 是打开 csv 文件的默认设置。

于 2013-07-31T13:05:38.467 回答