我有一个网格视图。我试图在excel中导出。 代码如下:
public override void VerifyRenderingInServerForm(Control gvReport1)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string attachment = "attachment; filename=Report.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
StringWriter stw = new StringWriter();
HtmlTextWriter htextw = new HtmlTextWriter(stw);
gvReport1.RenderControl(htextw);
Response.Write(stw.ToString());
Response.End();
}
现在的问题是excel文件正在打开,但数据没有显示出来。我猜数据没有被读取,更奇怪的是我有两个网页。在第一个网页中,导出到Excel的工作正常,但在第二个页面上它不起作用。
答案 0 :(得分:1)
可能有一种更优雅的方式......
有。
看看这个: Export to Excel library
这是一个完全免费的库,它使用Microsoft的Open XML库来创建真正的Excel 2007 .xlsx文件。
您所有所要做的就是调用一个“ CreateExcelDocument ”函数,然后传入DataTable,DataSet或List<>其中包含用于填充GridView的数据。
// Step 1: Create a DataSet, and put some sample data in it
DataSet ds = CreateSampleData();
// Step 2: Create the Excel .xlsx file
try
{
string excelFilename = "C:\\Sample.xlsx";
CreateExcelFile.CreateExcelDocument(ds, excelFilename);
}
catch (Exception ex)
{
MessageBox.Show("Couldn't create Excel file.\r\nException: " + ex.Message);
return;
}
享受,祝你好运!
答案 1 :(得分:0)
Response.Write(stw.ToString());
不正确,这只会返回类型名称,您需要先将RenderControl的输出写入字符串,然后使用Response.Write()写入字符串内容。
编辑:以下是如何获取渲染控件内容的示例:
// Obtain the content about to be rendered.
var ms = new MemoryStream();
var w = new StreamWriter(ms);
var r = new StreamReader(ms);
using (var htmlw = new HtmlTextWriter(w))
{
gvReport1.RenderControl(htmlw);
htmlw.Flush();
}
ms.Position = 0;
var content = r.ReadToEnd();
r.Close();
可能有一种更优雅的方式......
答案 2 :(得分:0)
我希望你能在这里找到你所搜索的内容:
.doc
application/msword
.dot
application/msword
.docx
application/vnd.openxmlformats-officedocument.wordprocessingml.document
.dotx
application/vnd.openxmlformats-officedocument.wordprocessingml.template
.docm
application/vnd.ms-word.document.macroEnabled.12
.dotm
application/vnd.ms-word.template.macroEnabled.12
.xls
application/vnd.ms-excel
.xlt
application/vnd.ms-excel
.xla
application/vnd.ms-excel
.xlsx
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.xltx
application/vnd.openxmlformats-officedocument.spreadsheetml.template
.xlsm
application/vnd.ms-excel.sheet.macroEnabled.12
.xltm
application/vnd.ms-excel.template.macroEnabled.12
.xlam
application/vnd.ms-excel.addin.macroEnabled.12
.xlsb
application/vnd.ms-excel.sheet.binary.macroEnabled.12
.ppt
application/vnd.ms-powerpoint
.pot
application/vnd.ms-powerpoint
.pps
application/vnd.ms-powerpoint
.ppa
application/vnd.ms-powerpoint
.pptx
application/vnd.openxmlformats-officedocument.presentationml.presentation
.potx
application/vnd.openxmlformats-officedocument.presentationml.template
.ppsx
application/vnd.openxmlformats-officedocument.presentationml.slideshow
.ppam
application/vnd.ms-powerpoint.addin.macroEnabled.12
.pptm
application/vnd.ms-powerpoint.presentation.macroEnabled.12
.potm
application/vnd.ms-powerpoint.presentation.macroEnabled.12
.ppsm
application/vnd.ms-powerpoint.slideshow.macroEnabled.12