我正在使用VS 2008,C#.net,asp.net。 我将数据导出到excel 2003。 导出的excel在IE和Chrome中的类型为.xls。 但是在mozilla中没有.xls类型。打开该文件时,它会询问您要打开哪个程序? 是什么原因?
程序如下所述。 ##标题##
private void ExportDataSetToExcel() { 尝试 { DataSet Ds = new DataSet(); DataTable DT = new DataTable();
string param = hidType.Get("Type").ToString();
string[] codes = param.Split('.');
if (codes.Length == 1)
FillGrid(Convert.ToString(param));
else
FillDrillDownGrid(codes[1], codes[0]);
if (hidType.Get("Type").ToString() == "2")
{
DT.Columns.Add("<b>Sub Group Code</b>");
DT.Columns.Add("<b>Account Sub Group</b>");
}
else if (hidType.Get("Type").ToString() == "3")
{
DT.Columns.Add("<b>Group Code</b>");
DT.Columns.Add("<b>Account Group</b>");
}
else
{
DT.Columns.Add("<b>Acc.Head Code</b>");
DT.Columns.Add("<b>Account Head</b>");
}
DT.Columns.Add("<b>Debit</b>");
DT.Columns.Add("<b>Credit</b>");
for (int j = 0; j < grdTrialBal.VisibleRowCount; j++)
{
DataRow DR;
DR = DT.NewRow();
if (hidType.Get("Type").ToString() == "2")
{
DR["<b>Sub Group Code</b>"] = grdTrialBal.GetRowValues(j, "ahdUserCode");
DR["<b>Account Sub Group</b>"] = grdTrialBal.GetRowValues(j, "ahdname");
}
else if (hidType.Get("Type").ToString() == "3")
{
DR["<b>Group Code</b>"] = grdTrialBal.GetRowValues(j, "ahdUserCode");
DR["<b>Account Group</b>"] = grdTrialBal.GetRowValues(j, "ahdname");
}
else
{
DR["<b>Acc.Head Code</b>"] = grdTrialBal.GetRowValues(j, "ahdUserCode");
DR["<b>Account Head</b>"] = grdTrialBal.GetRowValues(j, "ahdname");
}
if (Convert.ToDecimal(grdTrialBal.GetRowValues(j, "trnAhdDrAmt")) != 0)
{
DR["<b>Debit</b>"] = Convert.ToDecimal(grdTrialBal.GetRowValues(j, "trnAhdDrAmt"));
}
if (Convert.ToDecimal(grdTrialBal.GetRowValues(j, "trnAhdCrAmt")) != 0)
{
DR["<b>Credit</b>"] = Convert.ToDecimal(grdTrialBal.GetRowValues(j, "trnAhdCrAmt"));
}
CrAmount = CrAmount + Convert.ToDecimal(grdTrialBal.GetRowValues(j, "trnAhdCrAmt"));
DrAmount = DrAmount + Convert.ToDecimal(grdTrialBal.GetRowValues(j, "trnAhdDrAmt"));
DT.Rows.Add(DR);
}
DataRow Dr1;
Dr1 = DT.NewRow();
if (hidType.Get("Type").ToString() == "2")
{
Dr1["<b>Sub Group Code</b>"] = "";
Dr1["<b>Account Sub Group</b>"] = "<b>Total</b>";
}
else if (hidType.Get("Type").ToString() == "3")
{
Dr1["<b>Group Code</b>"] = "";
Dr1["<b>Account Group</b>"] = "<b>Total</b>";
}
else
{
Dr1["<b>Acc.Head Code</b>"] = "";
Dr1["<b>Account Head</b>"] = "<b>Total</b>";
}
Dr1["<b>Debit</b>"] = "<b>" + DrAmount.ToString("#0.#0") + "</b>";
Dr1["<b>Credit</b>"] = "<b>" + CrAmount.ToString("#0.#0") + "</b>";
DT.Rows.Add(Dr1);
DataGrid dg = new DataGrid();
dg.DataSource = DT;
dg.DataBind();
String strFileName = "Trial Balance Account Group Wise.xls";
if (hidType.Get("Type").ToString() == "2")
{
strFileName = "Trial Balance Account Sub Group Wise.xls";
}
else if (hidType.Get("Type").ToString() == "3")
{
strFileName = "Trial Balance Account Group Wise.xls";
}
else
{
strFileName = "Trial Balance Account Head Wise.xls";
}
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=" + strFileName);
Response.ContentType = "application/excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
CommonFunctions cf = new CommonFunctions();
String[] aTo = mskToDate1.Text.Split(new[] { '/', '-' });
String dTo;
dTo = aTo[0] + "/" + cf.getMonthName(Convert.ToInt32(aTo[1])) + "/" + aTo[2];
if (hidType.Get("Type").ToString() == "2")
{
htw.WriteLine("<b><u><font size='5'>" + "Trial Balance - Account Sub Group Wise - As on :" + dTo + " </font></u></b>");
}
else if (hidType.Get("Type").ToString() == "3")
{
htw.WriteLine("<b><u><font size='5'>" + "Trial Balance - Account Group Wise - As on :" + dTo + " </font></u></b>");
}
else
{
htw.WriteLine("<b><u><font size='5'>" + "Trial Balance - Account Head Wise - As on : " + dTo + " </font></u></b>");
}
dg.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
dg = null;
dg.Dispose();
}
catch (Exception ex)
{
}
}
答案 0 :(得分:0)
用双引号设置文件名
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + strFileName+ "\"");