我正在使用 Windows 窗体应用程序。我想使用 SaveAs(browse) 选项将我的 gridview 数据导出到 excel 中。
请建议。
我正在使用 Windows 窗体应用程序。我想使用 SaveAs(browse) 选项将我的 gridview 数据导出到 excel 中。
请建议。
这意味着当前HttpContext( HttpContext.Current) 是null。
这是因为这是一个 Windows 窗体应用程序,而不是 ASP.NET 网站。你不能这样做。
Windows 窗体应用程序中没有 HttpContext 所以在你的情况下这是空的
HttpContext.Current 不仅适用于 Web 应用程序,而且不为 null。在 Windows 应用程序中,它始终为空。
HttpContext或HttpContext.Current似乎为空。
You may use this code for exporting, because you are using a code intended for exporting asp.net gridviews:
public void export_datagridview_to_excel(DataGridView dgv, string excel_file)
{
int cols;
//open file
StreamWriter wr = new StreamWriter(excel_file);
//determine the number of columns and write columns to file
cols = dgv.Columns.Count;
for (int i = 0; i < cols; i++)
{
wr.Write(dgv.ColumnsIdea.Name.ToString().ToUpper() + "\t");
}
wr.WriteLine();
//write rows to excel file
for (int i = 0; i < (dgv.Rows.Count - 1); i++)
{
for (int j = 0; j < cols; j++)
{
if (dgv.RowsIdea.Cells[j].Value != null)
wr.Write(dgv.RowsIdea.Cells[j].Value + "\t");
else
{
wr.Write("\t");
}
}
wr.WriteLine();
}
//close file
wr.Close();
}