| |

VerySource

 Forgot password?
 Register
Search
View: 657|Reply: 3

How to import data from DataGrid to Excel?

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-20 18:30:02
| Show all posts |Read mode
How to import data from DataGrid to Excel? I also found some information on the Internet, but it is not possible. Why? Can you help?
Reply

Use magic Report

0

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-6-26 18:30:01
| Show all posts
StringWriter sw=new StringWriter();
sw.WriteLine("Column name 1, Column name 2, Column name 3");
The
foreach(DataRow dr in ds.Tables[0].Select("flag=0"))
{
sw.WriteLine(dr[0]+","+dr[1]+","+dr[2]);
}
             sw.Close();
Response.AddHeader("Content-Disposition", "attachment; filename=eBay.csv");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding=System.Text.Encoding.GetEncoding("UTF-8");
Response.Write(sw);
Response.End();
This is out to csv format text, excel is basically similar to this, hope helps you a bit!
Reply

Use magic Report

0

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-6-29 16:30:01
| Show all posts
private void btnMIME_Click(object sender, System.EventArgs e)
{
BindData();

Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "inline;filename="
+ HttpUtility.UrlEncode("Download file.xls",Encoding.UTF8) );
The

//If the output is Word, modify to the following code
//Response.ContentType = "application/ms-word"
//Response.AddHeader("Content-Disposition", "inline;filename=test.doc")
StringBuilder sb=new StringBuilder();
System.IO.StringWriter sw = new System.IO.StringWriter(sb);
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
sb.Append("<html><body>");
dgShow.RenderControl(hw);
sb.Append("</body></html>");
Response.Write(sb.ToString());
Response.End();
}

private void btnCom_Click(object sender, System.EventArgs e)
{
ExportToExcel(BindData(),Server.MapPath("ComExcel.xls"));
The
}
//From DataSet to Excel
#region From DataSet to Out to Excel
/// Export the specified Excel file
public void ExportToExcel(DataSet ds,string strExcelFileName)
{
if (ds.Tables.Count==0 || strExcelFileName=="") return;
doExport(ds,strExcelFileName);
   

}
/// Perform export
private void doExport(DataSet ds,string strExcelFileName)
{
            
Excel.Application excel = new Excel.Application();
int rowIndex=1;
int colIndex=0;
excel.Application.Workbooks.Add(true);
System.Data.DataTable table=ds.Tables[0];
foreach(DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[1,colIndex]=col.ColumnName;
}

foreach(DataRow row in table.Rows)
{
rowIndex++;
colIndex=0;
foreach(DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
}
}
excel.Visible=false;
excel.ActiveWorkbook.SaveAs(strExcelFileName+".XLS",Excel.XlFileFormat.xlExcel9795,null,null,false,false,Excel.XlSaveAsAccessMode.xlNoChange,null,null,null,null,null);
excel.Quit();
excel=null;
GC.Collect();//Garbage collection
}
        #endregion
}
Reply

Use magic Report

0

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-6-29 16:45:01
| Show all posts
Tested, certain
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list