Hi,
I have data in a Data Table. Now I need this data to written in Excel File 2003 . I need to create a New File and Write the Data and close the file.
I need the code. Please help me.
Regards,
Mzee
Hi,
I have data in a Data Table. Now I need this data to written in Excel File 2003 . I need to create a New File and Write the Data and close the file.
I need the code. Please help me.
Regards,
Mzee
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Timothy FosterPosted Feb 26, 2013, 3:21 AM
If you create a commercial product you may want to use a 3rd party component to spead up your work. For example take a look at this C# Excel Writer. This code shows how to export datatable to a new excel worksheet and save result file to disk:
DataTable dt = new DataTable();
ExcelWorkbook wb = new ExcelWorkbook();
wb.Worksheets.Add("sh1");
wb.Worksheets[0].ReadFromDataTable(dt);
wb.WriteXLS(@"c:\output.xls");
Abhijit baruaPosted Aug 7, 2012, 8:10 AM
protected void ExportToPDF(object sender, EventArgs e)
{
//Get the data from database into datatable
string strQuery = "select empid, empname, city, postalcode" +
" from employee";
SqlCommand cmd = new SqlCommand(strQuery);
DataTable dt = GetData(cmd);
//Create a dummy GridView
GridView GridView1 = new GridView();
GridView1.AllowPaging = false;
GridView1.DataSource = dt;
GridView1.DataBind();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition",
"attachment;filename=abc.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
Santhosh Kumar JayaramanPosted Aug 7, 2012, 8:01 AM
http://www.c-sharpcorner.com/UploadFile/1a81c5/export-datatable-to-excel-with-formatting-in-C-Sharp/