html excel导出的路径,C#导出生成excel文件的方法小结(xml,html方式)

///

/// xml格式生成excel文件并存盘;

///

/// 生成报表的页面,没有传null

/// 数据表

/// 报表标题,sheet1名

/// 存盘文件名,全路径

/// 生成文件后是否提示下载,只有web下才有效

public static void CreateExcelByXml(System.Web.UI.Page page, DataTable dt, String TableTitle, string fileName, bool IsDown)

{

StringBuilder strb = new StringBuilder();

strb.Append("

strb.Append("xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");

strb.Append("xmlns=\"");

strb.Append("

");

strb.Append("

strb.Append("body");

strb.Append(" {mso-style-parent:style0;");

strb.Append(" font-family:\"Times New Roman\", serif;");

strb.Append(" mso-font-charset:0;");

strb.Append(" mso-number-format:\"@\";}");

strb.Append("table");

//strb.Append(" {border-collapse:collapse;margin:1em 0;line-height:20px;font-size:12px;color:#222; margin:0px;}");

strb.Append(" {border-collapse:collapse;margin:1em 0;line-height:20px;color:#222; margin:0px;}");

strb.Append("thead tr td");

strb.Append(" {background-color:#e3e6ea;color:#6e6e6e;text-align:center;font-size:14px;}");

strb.Append("tbody tr td");

strb.Append(" {font-size:12px;color:#666;}");

strb.Append(" ");

strb.Append(" ");

strb.Append(" ");

strb.Append(" ");

strb.Append(" ");

//设置工作表 sheet1的名称

strb.Append(" " + TableTitle + " ");

strb.Append(" ");

strb.Append(" 285 ");

strb.Append(" ");

strb.Append(" ");

strb.Append(" ");

strb.Append(" 3 ");

strb.Append(" 1 ");

strb.Append(" ");

strb.Append(" ");

strb.Append(" False ");

strb.Append(" False ");

strb.Append(" False ");

strb.Append(" ");

strb.Append(" ");

strb.Append(" 6750 ");

strb.Append(" 10620 ");

strb.Append(" 480 ");

strb.Append(" 75 ");

strb.Append(" False ");

strb.Append(" False ");

strb.Append(" ");

strb.Append(" ");

strb.Append("");

strb.Append("

");

strb.Append("

//合格所有列并显示标题

strb.Append("

");

strb.Append(TableTitle);

strb.Append("

");

strb.Append("

");

strb.Append("

");

if (dt != null)

{

//写列标题

int columncount = dt.Columns.Count;

for (int columi = 0; columi < columncount; columi++)

{

strb.Append("

" + dt.Columns[columi] + " ");

}

strb.Append("

");

//写数据

for (int i = 0; i < dt.Rows.Count; i++)

{

strb.Append("

");

for (int j = 0; j < dt.Columns.Count; j++)

{

strb.Append("

" + dt.Rows[i][j].ToString() + " ");

}

strb.Append("

");

}

}

strb.Append("

");

strb.Append(" ");

string ExcelFileName = fileName;

//string ExcelFileName = Path.Combine(page.Request.PhysicalApplicationPath, path+"/guestData.xls");

//报表文件存在则先删除

if (File.Exists(ExcelFileName))

{

File.Delete(ExcelFileName);

}

StreamWriter writer = new StreamWriter(ExcelFileName, false);

writer.WriteLine(strb.ToString());

writer.Close();

//如果需下载则提示下载对话框

if (IsDown)

{

DownloadExcelFile(page, ExcelFileName);

}

}

---------

///

/// web下提示下载

///

///

/// 文件名,全路径

public static void DownloadExcelFile(System.Web.UI.Page page, string FileName)

{

page.Response.Write("path:" + FileName);

if (!System.IO.File.Exists(FileName))

{

MessageBox.ShowAndRedirect(page, "文件不存在!", FileName);

}

else

{

FileInfo f = new FileInfo(FileName);

HttpContext.Current.Response.Clear();

HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + f.Name);

HttpContext.Current.Response.AddHeader("Content-Length", f.Length.ToString());

HttpContext.Current.Response.AddHeader("Content-Transfer-Encoding", "binary");

HttpContext.Current.Response.ContentType = "application/octet-stream";

HttpContext.Current.Response.WriteFile(f.FullName);

HttpContext.Current.Response.End();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值