求一个实例,将GridView中数据导入到Excel表中(在线等)
如题!要一个完整的实例,能运行!
[解决办法]
看看这个可以吗:http://blog.csdn.net/hhxxcj/archive/2009/01/14/3772552.aspx
[解决办法]
http://www.cnblogs.com/activer/archive/2006/08/27/487749.html
[解决办法]
如果是分页的情况下:
using OWC;
using System.Data.OleDb;
using System.IO;
SpreadsheetClass xlsheet=new SpreadsheetClass();
conn.Open();
cmd=new OleDbCommand(sql,conn);
OleDbDataReader reader=cmd.ExecuteReader();
int numbercols=reader.FieldCount;
int row=2;
int i=0;
//输出标题
for(i=0;i <numbercols;i++)
{
xlsheet.ActiveSheet.Cells[1, i + 1] = reader.GetName(i).ToString();
}
// 输出字段内容
while(reader.Read())
{
for(i=0;i <numbercols;i++)
{
xlsheet.ActiveSheet.Cells[row, i + 1] = reader.GetValue(i).ToString();
}
row = row + 1;
}
reader.Close();
conn.Close();
try
{
xlsheet.ActiveSheet.Export(Server.MapPath( ". ") + "\\mfExcel\\ " + this.xlfile.Text+ ".xls ",OWC.SheetExportActionEnum.ssExportActionNone);
}
catch(System.Runtime.InteropServices.COMException e )
{
Response.Write( "错误: " + e.Message);
}
[解决办法]
http://www.tzwhx.com/newOperate/html/1/11/111/493.html
[解决办法]
[解决办法]
public partial class GridViewToExcel : System.Web.UI.Page{ GridView gridToExport = null; protected void Page_Load(object sender, EventArgs e) { } /// <summary> /// 必须override这个方法,否则会报错 /// /// "Control 'ctl00_MainContent_Gridview1' of type 'GridView' must be placed /// inside a form tag with runat=server", /// even though you have the supposedly missing form tag. /// /// </summary> /// <param name="control"></param> public override void VerifyRenderingInServerForm(Control control) { //base.VerifyRenderingInServerForm(control); } protected void btnExport_Click(object sender, EventArgs e) { gridToExport = gvwEmployee; } protected override void Render(HtmlTextWriter writer) { if (gridToExport as GridView != null) { ExportGridToExcel(gridToExport, "test.xls"); } base.Render(writer); } private void ExportGridToExcel(GridView grid, string filename) { // 检查是否有文件名 if (string.IsNullOrEmpty(filename)) { throw new ArgumentException("Export filename is required"); } // 检查是否是excel格式 if (!filename.EndsWith(".xls")) { filename += ".xls"; } grid.AllowPaging = false; // 禁止分页 grid.AllowSorting = false; // 禁止排序 grid.DataBind(); // 绑定数据 StringWriter tw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); Response.Clear(); Response.ContentType = "application/vnd.ms-excel"; Response.AddHeader("content-disposition", "attachment;filename=" + filename); Response.Charset = string.Empty; Page.EnableViewState = false; grid.RenderControl(hw); Response.Write(tw.ToString()); Response.End(); }}
[解决办法]
dataTime = dataTime.Replace("-","");
dataTime = dataTime.Replace(".","");
dataTime = dataTime.Replace("_","");
dataTime = dataTime.Substring(0, 17);
string Filename = dataTime +SetRadomNum();
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + Filename + ".csv");
System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
System.Web.HttpContext.Current.Response.Write(sw);
sw.Flush();
System.Web.HttpContext.Current.Response.End();
}