asp.net导出excel 打开揭示格式不一致
asp.net导出excel 打开提示格式不一致
先上代码:
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.AddHeader("Content-Transfer-Encoding", "binary");
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
String FileName = "ToExcel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName + ".xls");
HttpContext.Current.Response.Write("<?xml version=\"1.0\"?>");
HttpContext.Current.Response.Write("<?mso-application progid=\"Excel.Sheet\"?>");
HttpContext.Current.Response.Write("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"");
HttpContext.Current.Response.Write(" xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
HttpContext.Current.Response.Write(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
HttpContext.Current.Response.Write(" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"");
HttpContext.Current.Response.Write(" xmlns:html=\"http://www.w3.org/TR/REC-html40/\">");
.......
我是用了http输出流 输出xml格式的excel,导出后打开提示:你尝试打开的文件XXX格式与文件扩展名格式指定格式不一致。虽然能正常看到数据,但是这个提示实在不友好,我上网找到的方法是修改客户端电脑的注册表,我开发这个网站是面向客户,我不可能要求每个客户的电脑修改注册表,请求各位有什么方法解决这样的问题?
------解决方案--------------------
...额 具体代码这个说不清楚。。貌似用ajax不能输出内存流 这个不清楚。
但是我是这样子的具体思路是这样的
<div id="loadExcel">
<form id="Excelform" runat="server" action="文件名.xls">
<asp:Button ID="btnExcel" runat="server" Text="Button" />
</form>
</div>
然后action.指定为你文件名字
然后在Global.asax里面的Application_BeginRequest方法里面写导出excel的代码。我贴我自己的
string fullOrigionalpath = Request.Url.ToString();
if (fullOrigionalpath.Contains("PurchaseDetail.xls"))
{
string OrderID = HttpContext.Current.Request.Form["Orderno"];
string jch = HttpContext.Current.Request.Form["txtJch"];
if (string.IsNullOrEmpty(OrderID)
------解决方案--------------------
string.IsNullOrEmpty(jch))
{
return;
}
//获取全部供应商简称
System.Data.DataTable PurSupp = new PurchaseBtbBLL().GetListjch("Amain=" + OrderID + "");
//获取供应商信息
DataTable Suppdt = new SupplierBLL().GetSuppByJc(jch);
//获取小单集合
DataTable Purchasedt = new PurchaseBtbBLL().GetListEN("Amain='" + OrderID + "' and Jch='" + jch + "'");
//添加条形码列
DataColumn dataColumn = new DataColumn();
dataColumn.DataType = typeof(string);
dataColumn.ColumnName = "Barcode3";
Purchasedt.Columns.Add(dataColumn);
for (int j = 0; j < Purchasedt.Rows.Count; j++)
{
Purchasedt.Rows[j]["Barcode3"] = "48941280" + Purchasedt.Rows[j]["Barcode"].ToString().PadLeft(4, '0') + Purchasedt.Rows[j]["Barcode2"].ToString();
}
先上代码:
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.AddHeader("Content-Transfer-Encoding", "binary");
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
String FileName = "ToExcel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName + ".xls");
HttpContext.Current.Response.Write("<?xml version=\"1.0\"?>");
HttpContext.Current.Response.Write("<?mso-application progid=\"Excel.Sheet\"?>");
HttpContext.Current.Response.Write("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"");
HttpContext.Current.Response.Write(" xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
HttpContext.Current.Response.Write(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
HttpContext.Current.Response.Write(" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"");
HttpContext.Current.Response.Write(" xmlns:html=\"http://www.w3.org/TR/REC-html40/\">");
.......
我是用了http输出流 输出xml格式的excel,导出后打开提示:你尝试打开的文件XXX格式与文件扩展名格式指定格式不一致。虽然能正常看到数据,但是这个提示实在不友好,我上网找到的方法是修改客户端电脑的注册表,我开发这个网站是面向客户,我不可能要求每个客户的电脑修改注册表,请求各位有什么方法解决这样的问题?
------解决方案--------------------
...额 具体代码这个说不清楚。。貌似用ajax不能输出内存流 这个不清楚。
但是我是这样子的具体思路是这样的
<div id="loadExcel">
<form id="Excelform" runat="server" action="文件名.xls">
<asp:Button ID="btnExcel" runat="server" Text="Button" />
</form>
</div>
然后action.指定为你文件名字
然后在Global.asax里面的Application_BeginRequest方法里面写导出excel的代码。我贴我自己的
string fullOrigionalpath = Request.Url.ToString();
if (fullOrigionalpath.Contains("PurchaseDetail.xls"))
{
string OrderID = HttpContext.Current.Request.Form["Orderno"];
string jch = HttpContext.Current.Request.Form["txtJch"];
if (string.IsNullOrEmpty(OrderID)
------解决方案--------------------
string.IsNullOrEmpty(jch))
{
return;
}
//获取全部供应商简称
System.Data.DataTable PurSupp = new PurchaseBtbBLL().GetListjch("Amain=" + OrderID + "");
//获取供应商信息
DataTable Suppdt = new SupplierBLL().GetSuppByJc(jch);
//获取小单集合
DataTable Purchasedt = new PurchaseBtbBLL().GetListEN("Amain='" + OrderID + "' and Jch='" + jch + "'");
//添加条形码列
DataColumn dataColumn = new DataColumn();
dataColumn.DataType = typeof(string);
dataColumn.ColumnName = "Barcode3";
Purchasedt.Columns.Add(dataColumn);
for (int j = 0; j < Purchasedt.Rows.Count; j++)
{
Purchasedt.Rows[j]["Barcode3"] = "48941280" + Purchasedt.Rows[j]["Barcode"].ToString().PadLeft(4, '0') + Purchasedt.Rows[j]["Barcode2"].ToString();
}