我使用以下代码将数据集转换为 xls。它工作正常,但会引发错误。(一些格式问题)
我想在未经用户许可的情况下保存在特定位置创建的 xls 文件。
conn.Open();
SqlDataAdapter getData = new SqlDataAdapter("SELECT * FROM profile", conn);
data.Clear();
getData.Fill(data);
conn.Close();
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.Charset = "";
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=\"" +"siv" +"\"");
using (StringWriter sw = new StringWriter())
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
DataGrid dg = new DataGrid();
dg.DataSource = data.Tables[0];
dg.DataBind();
dg.RenderControl(htw);
response.Write(sw.ToString());
response.End();
}