这里我用gridview数据创建excel文件,代码如下
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
//Change the Header Row back to white color
GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
//Applying stlye to gridview header cells
for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
{
GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#df5015");
}
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
它运行正常,但是如果我将gridview属性设置为 AllowPaging =&#34; true&#34; ,excel文件只包含分页1数据,这里我如何导出从gridview到excel的所有数据。
答案 0 :(得分:0)
在您的aspx页面中,设置网格的 AllowPaging =“true”
在您的代码隐藏文件中更改您的代码,
GridView1.AllowPaging = false;
// BIND YOUR GRID HERE AGAIN
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
//Change the Header Row back to white color
GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
//Applying stlye to gridview header cells
for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
{
GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#df5015");
}
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
答案 1 :(得分:0)
我假设您使用存储在public string GetImagefromWord(string filePath)
{
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
object miss = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(filePath, miss, miss, miss, miss);
foreach (Microsoft.Office.Interop.Word.Row aRow in docs.Tables[0].Rows)
{
foreach (Microsoft.Office.Interop.Word.Cell aCell in aRow.Cells)
{
string xml = aCell.Range.XML;
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(xml);
****//xpictNode-Node of word XML having Image****
string imageType = xpictNode.Attributes["w:name"].Value.Substring(xpictNode.Attributes["w:name"].Value.LastIndexOf('.') + 1);
string path = Path.GetDirectoryName(Application.ExecutablePath);
byte[] byteBuffer = Convert.FromBase64String(xdoc.xpictNode.InnerText);
MemoryStream memoryStream = new MemoryStream(byteBuffer);
string img = " <img src= data:image/bmp;base64," + Convert.ToBase64String(File.ReadAllBytes(GetImageString(memoryStream, path, imageType))) + " />";
//Function GetImageString() is shown above
}
}
}
,GridView
或DataSet
中的数据填充DataTable
?
如果是这样,只需获取该变量,并将其传递给我的“导出到Excel”C#库,它将为您创建一个真实的.xlsx文件,并可以将其直接传递到您的网页List<>
Response
可以在此处找到“导出到Excel”类的完整源代码:
您不希望这样做的唯一原因是,如果您特别希望自己的Excel包含class Employee;
List<Employee> listOfEmployees = new List<Employee>();
...
GridView1.DataSource = listOfEmployees;
...
// The following ASP.Net code gets run when I click on my "Export to Excel" button.
protected void btnExportToExcel_Click(object sender, EventArgs e)
{
// It doesn't get much easier than this...
CreateExcelFile.CreateExcelDocument(listOfEmployees, "Employees.xlsx", Response);
}
中显示的值,而不是通过GridView
修改它们功能
我的建议只会导出绑定到GridView的原始数据。