別のフォーラムに質問を投稿しようとしましたが、まだ応答がありません。誰かが本当に私を助けてくれますか?ネストされたグリッドビューのエクスポートで収集したほとんどのコーディングを試しましたが、ほとんどのコーディングで同じ出力が得られました。実際、必要なのは非常に「シンプル」でした。
コード(Excel)
protected void Export(GridView gridView)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "StaffAppraisal.xls"));
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
// Create a table to contain the grid
Table table = new Table();
// include the gridline settings
table.GridLines = gridView.GridLines;
// add the header row to the table
if (gridView.HeaderRow != null)
{
PrepareControlForExport(gridView.HeaderRow);
gridView.HeaderRow.Style.Add("background-color", "");
table.Rows.Add(gridView.HeaderRow);
}
// add each of the data rows to the table
foreach (GridViewRow row in gridView.Rows)
{
PrepareControlForExport(row);
table.Rows.Add(row);
}
// add the footer row to the table
if (gridView.FooterRow != null)
{
PrepareControlForExport(gridView.FooterRow);
table.Rows.Add(gridView.FooterRow);
}
for (int i = 0; i <= gridView.Rows.Count; i++)
{
table.Rows[i].Cells[0].Visible = false;
}
using (StringWriter stringWriter = new StringWriter())
{
using (HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter))
{
// render the table into the htmlwriter
table.RenderControl(htmlWriter);
// render the htmlwriter into the response
HttpContext.Current.Response.Write(stringWriter.ToString());
HttpContext.Current.Response.End();
}
}
}
コード(単語)
public static void Export(GridView gridView)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "StaffAppraisal.doc"));
HttpContext.Current.Response.ContentType = "application/ms-word";
// Create a table to contain the grid
Table table = new Table();
// include the gridline settings
table.GridLines = gridView.GridLines;
// add the header row to the table
if (gridView.HeaderRow != null)
{
PrepareControlForExport(gridView.HeaderRow);
table.Rows.Add(gridView.HeaderRow);
}
for (int j = 0; j < gridView.Columns.Count; j++)
{
//Apply style to Individual Cells
gridView.HeaderRow.Cells[j].Style.Add("background-color", "black");
}
// add each of the data rows to the table
foreach (GridViewRow row in gridView.Rows)
{
PrepareControlForExport(row);
table.Rows.Add(row);
}
// add the footer row to the table
if (gridView.FooterRow != null)
{
PrepareControlForExport(gridView.FooterRow);
table.Rows.Add(gridView.FooterRow);
}
for (int i = 0; i <= gridView.Rows.Count; i++)
{
table.Rows[i].Cells[0].Visible = false;
}
using (StringWriter stringWriter = new StringWriter())
{
using (HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter))
{
// render the table into the htmlwriter
table.RenderControl(htmlWriter);
// render the htmlwriter into the response
HttpContext.Current.Response.Write(stringWriter.ToString());
HttpContext.Current.Response.End();
}
}
}
下の画像はExcelからエクスポートされています...
下の画像はWordからエクスポートされたものです...