Hi, I need to export my datagridview to excel; is there something I can use for that? I installed AccessDatabaseEngine to use
interp.excel but the application not start with this reference.
With the NPOI classes I managed to create a “stream” of datagridview and download it as xls file
You can use ExcelLibrary.dll from https://code.google.com/archive/p/excellibrary/downloads
and use code to export .xls file
string filename = HttpContext.Current.Server.MapPath(“export.xls”);
DataTable dt = new DataTable();
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
dt.Columns.Add(col.HeaderText);
}
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataRow dRow = dt.NewRow();
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.Value != null)
{
dRow[cell.ColumnIndex] = cell.Value.ToString();
}else
{
dRow[cell.ColumnIndex] = “”;
}
}
dt.Rows.Add(dRow);
}
DataSet ds1 = new DataSet();
ds1.Tables.Add(dt);
ds1.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;
//Here’s the easy part. Create the Excel worksheet from the data set
ExcelLibrary.DataSetHelper.CreateWorkbook(filename, ds1);
Application.Download(filename);
ขอบคุณครับ
You can use ClosedXML:
https://github.com/ClosedXML/ClosedXML
Lots of examples if you Google it. This will create actual Excel or Word documents.
You can use the built-in DataGridView.GetClipboardContent(), it will return HTML that can be opened by Excel. Look also at ClipboardCopyMode:
https://wisej.com/docs/html/P_Wisej_Web_DataGridView_ClipboardCopyMode.htm
https://wisej.com/docs/html/M_Wisej_Web_DataGridView_GetClipboardContent.htm
Here is a video showing the same functionality copied to excel:
http://www.screencast.com/t/76izqzD8F
Please login first to submit.