是的,我已经制作了这个代码,我已经尝试将其与当前的网页分开并且它工作得很好但是当我将它与我的网络结合时,我没有得到gridview中生成的输出。这是我用过的代码。
protected void Wordbtn_Click(object sender, EventArgs e)
{
if (maxdata.Checked == true)
{
wordmax();
}
if (curdata.Checked == true)
{
wordcur();
}
}
protected void excelbtn_Click(object sender, EventArgs e)
{
if (maxdata.Checked == true)
{
excelmax();
}
if (curdata.Checked == true)
{
excelcur();
}
}
protected void pdfbtn_Click(object sender, EventArgs e)
{
if (maxdata.Checked == true)
{
pdfmax();
}
if (curdata.Checked == true)
{
pdfcur();
}
}
public void wordmax()
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=MaxdataExport.doc");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-word ";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gridmaxdata.AllowPaging = false;
gridmaxdata.DataBind();
gridmaxdata.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
public void wordcur()
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=CurrentDataExport.doc");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-word ";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gridcurdata.AllowPaging = false;
gridcurdata.DataBind();
gridcurdata.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
public void excelmax()
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition",
"attachment;filename=MaxDataExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gridmaxdata.AllowPaging = false;
gridmaxdata.DataBind();
//Change the Header Row back to white color
gridmaxdata.HeaderRow.Style.Add("background-color", "#FFFFFF");
gridmaxdata.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
public void excelcur()
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition",
"attachment;filename=CurrentdataExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gridcurdata.AllowPaging = false;
gridcurdata.DataBind();
//Change the Header Row back to white color
gridcurdata.HeaderRow.Style.Add("background-color", "#FFFFFF");
gridcurdata.RenderControl(hw);
//---------------------------------------------------------------------------------
//style to format numbers to string
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
public void pdfmax()
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=MaxDataExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gridmaxdata.AllowPaging = false;
gridmaxdata.DataBind();
gridmaxdata.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
public void pdfcur()
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=CurrentdataExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gridcurdata.AllowPaging = false;
gridcurdata.DataBind();
gridcurdata.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
这是我用来创建datagridview的代码
public void maxdatatable()
{
string connectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\inetpub\\wwwroot\\webradiation\\App_Data\\Radiation.mdf;Integrated Security=True;User Instance=True";
//Bind SQLDataSource to GridView for max data
// Create SQLDataSource.
SqlDataSource sqlDataSource1 = new SqlDataSource();
sqlDataSource1.ID = "SqlDataSource123";
this.Page.Controls.Add(sqlDataSource1);
// Bind ConnectionString to SQLDataSource.
sqlDataSource1.ConnectionString = connectionString;
// Retrieve records
sqlDataSource1.SelectCommand = "SELECT top 30 [date], [data] FROM [loc1] WHERE (([data] >= '2') AND (([date] >= '" + combdatetime11.ToString() + "') AND ([date] <= '" + combdatetime21.ToString() + "'))) ORDER BY [data] DESC, [date] DESC";
gridmaxdata.DataSource = sqlDataSource1;
gridmaxdata.DataBind();
}
public void currdata()
{
string connectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\inetpub\\wwwroot\\webradiation\\App_Data\\Radiation.mdf;Integrated Security=True;User Instance=True";
// Create SQLDataSource.
SqlDataSource sqlDataSource2 = new SqlDataSource();
sqlDataSource2.ID = "SqlDataSource12";
this.Page.Controls.Add(sqlDataSource2);
// Bind ConnectionString to SQLDataSource.
sqlDataSource2.ConnectionString = connectionString;
// Retrieve records
sqlDataSource2.SelectCommand = "SELECT [date], [data] FROM [loc1] WHERE (([date] >= '" + combdatetime11.ToString() + "') AND ([date] < '" + combdatetime21.ToString() + "'))";
gridcurdata.DataSource = sqlDataSource2;
gridcurdata.DataBind();
}
对于word文档,我得到的输出是
(div) (/div)
*我必须用“(”和“)”代替“&lt;”和“&gt;”因为这个pageweb让它消失了
excel就像这样
(style) .textmode { mso-number-format:\@; } (/style)(div)(div)
pdf文件我收到一条错误消息,说该文档没有页面。
你不知道我哪里做错了吗?
答案 0 :(得分:0)
你可以创建一个扩展函数来将数据源数据表转换为excel,然后使用Response.WriteFile
方法将其发送到客户端
using OfficeOpenXml;
public static class Extensions
{
public static void ToExcel(this DataTable source, string destinationXlsxPath)
{
using (ExcelPackage pck = new ExcelPackage())
{
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Data");
ws.Cells["A1"].LoadFromDataTable(source, true);
FileInfo file = new FileInfo(destinationXlsxPath);
pck.SaveAs(file);
}
}
}
EPPlus:http://epplus.codeplex.com/