The Artima Developer Community
Sponsored Link •

C# Answers Forum
Work with Excel from C#

2 replies on 1 page. Most recent reply: Jan 17, 2006 12:36 PM by Will He

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Prabakar

Posts: 2
Nickname: prabakar
Registered: Aug, 2005

Work with Excel from C# Posted: Aug 9, 2005 9:46 PM
Reply to this message Reply
Advertisement
Hi guys,

I am in need to take a report of content of a datagrid in a excel sheet.
How can i do this.
To work with Excel which namespace,assembly i have to refer.
Please guide me on this.

Thank you.


Deepak Roy

Posts: 1
Nickname: roy007
Registered: Jan, 2006

Re: Work with Excel from C# Posted: Jan 17, 2006 1:28 AM
Reply to this message Reply
Pass Your Page, Dataset and the Gridview to this function
-----------------------------------------------------------
public void ExportToExcel(Page pg,DataSet DsModule,GridView Gv)
{

string FileN;
FileN = "report1.xls";
// Set the content type to Excel
pg.Response.ClearContent();
pg.Response.ClearHeaders();
pg.EnableViewState = false;
pg.Response.Clear();
pg.Response.ContentType = "application/vnd.ms-excel";
pg.Response.AddHeader("Content-Disposition", "attachment;filename=" + FileN);
pg.EnableViewState = false;
//'Remove the charset from the Content-Type header
pg.Response.Charset = "";
System.IO.StringWriter myTextWriter = new System.IO.StringWriter();
HtmlTextWriter myHtmlTextWriter = new HtmlTextWriter(myTextWriter);
//'Get the HTML for the control
DataGrid dg = new DataGrid();
DataView dv = new DataView(DsModule.Tables[0]);
int i = 0;
foreach (DataColumn dc in DsModule.Tables[0].Columns)
{
dv.Table.Columns.ColumnName = Gv.Columns.ToString();
i++;
}
dg.DataSource = dv;
dg.HeaderStyle.Font.Bold = true;
dg.DataBind();
dg.RenderControl(myHtmlTextWriter);
//'Write the HTML to the browser
pg.Response.Write(myTextWriter.ToString());
//'End the response
pg.Response.End();
}

Will He

Posts: 3
Nickname: igotit
Registered: Jan, 2006

Re: Work with Excel from C# Posted: Jan 17, 2006 12:36 PM
Reply to this message Reply
1.create a reference in your project to Excel 9.0 Object library
2.Declare a Excel Application Object. For example:
private Excel.ApplicationClass() ExcelApp = null;
....
ExcelApp = new Excel.ApplicationClass();
3.In your program, using this Object to process Excel file.
For example:
......
Excel.Workbook outBook = ExcelApp.Workbooks.Add(Excel.XLWBATemplate.xlWBATWorksheet);

outBook.Worksheets.Equals( fileNameWithPath);
Excel.Worksheet TheSheet = (Excel.Worksheet)outBook.Worksheets[1];

.......set your datagride to the excel file
4. Write to excel file
For example,
....
TheSheet.Cells[rowCount, column] = yourValue;
rowCount++;
....

Flat View: This topic has 2 replies on 1 page
Topic: run program in another computer Previous Topic   Next Topic Topic: New BPM Tool - NetBpm

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use