尝试使用Gridview:
private void ExportToExcel() {
try {
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=OverallReport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using(StringWriter sw = new StringWriter()) {
HtmlTextWriter hw = new HtmlTextWriter(sw);
//To Export all pages
dgvTrendChart.AllowPaging = false;
this.SearchOverallList();
dgvTrendChart.HeaderRow.BackColor = Color.White;
foreach(TableCell cell in dgvTrendChart.HeaderRow.Cells) {
cell.BackColor = dgvTrendChart.HeaderStyle.BackColor;
}
for (int rowIndex = dgvTrendChart.Rows.Count - 2; rowIndex >= 0; rowIndex--) {
GridViewRow row = dgvTrendChart.Rows[rowIndex];
GridViewRow previousRow = dgvTrendChart.Rows[rowIndex + 1];
for (int i = 0; i < row.Cells.Count; i++) {
if (i != 3) {
if (row.Cells[i].Text == previousRow.Cells[i].Text) {
row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 :
previousRow.Cells[i].RowSpan + 1;
previousRow.Cells[i].Visible = false;
row.Cells[i].HorizontalAlign = HorizontalAlign.Center;
row.Cells[i].VerticalAlign = VerticalAlign.Top;
}
}
}
}
dgvTrendChart.RenderControl(hw);
string style = @ "<style>.textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
} catch {
//throw new Exception("");
}
}