110,538
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Public
{
public static class ToExcel
{
//将GridView数据导出至Excel中
public static string GridViewToExcel(GridView gd, string strPath, string strFileName)
{
new Help().CreateFolder(@"C:\reportforms");
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gd.RenderControl(htw);
string strHtml = sw.ToString().Trim();
string ExcelFileName = strFileName;
string FilePhysicialPathName = strPath; //Request.PhysicalApplicationPath;
//生成的Excel文件名
string objectExcelFileName = Path.Combine(FilePhysicialPathName, ExcelFileName);
if (File.Exists(objectExcelFileName))
{
File.Delete(objectExcelFileName);
}
FileStream fs = new FileStream(objectExcelFileName, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs, Encoding.GetEncoding("GB18030"));
bw.Write(strHtml);
bw.Close();
fs.Close();
return "";
}
}
}