如何将GridView中的数据导入到已设置好格式的Excel(模板)中去,并且供下载???
如何将GridView中的数据导入到指定好格式的Excel中去,并且供下载???例如在服务器的文件夹中存放了一个设置好格式的Excel文件(相当于模板),想实现将如何将GridView中的数据导入到设置好格式的这个Excel中去,并且供给客户端下载???
[[italic] 本帖最后由 foshan 于 2007-12-5 13:02 编辑 [/italic]]
Response.ClearContent(); Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls"); Response.ContentType = "application/excel"; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); GridView.RenderControl(htw); Response.Write(sw.ToString()); Response.End();
protected void ExportToExcel() { string style = @"<style> .text { mso-number-format:\@; } </script> "; //这一句是保存gridview的样式 Response.ClearContent(); Response.AddHeader("content-disposition", "attachment; filename=excel.xls"); Response.ContentType = "application/excel"; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); gridview.RenderControl(htw); Response.Write(style); Response.Write(sw.ToString()); }