Welcome to Dream.In.Code
Become an Expert!

Join 150,047 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,685 people online right now. Registration is fast and FREE... Join Now!




excel download

 
Reply to this topicStart new topic

excel download

sunrobin23
11 Feb, 2008 - 03:32 AM
Post #1

New D.I.C Head
*

Joined: 11 Feb, 2008
Posts: 1

hi

i have done export data in excel to use Microsoft Library object 9.0 .
i created new work book with this code


CODE

public void OpenReportTempalte()
{
if (m_oExcelApp != null)
CloseReportTemplate();

m_oExcelApp = new ApplicationClass();
//new workboos
Books = (Excel.Workbooks)m_oExcelApp.Workbooks;

Book = Books.Open(sReportTemplate, m_oMissing, m_oMissing,
m_oMissing, m_oMissing, m_oMissing, m_oMissing, m_oMissing, m_oMissing,
m_oMissing, m_oMissing, m_oMissing, m_oMissing);
sheets = (Excel.Sheets)Book.Worksheets;
worksheetSheet = (Excel.Worksheet)sheets.get_Item(1);
cell = (Excel.Range)worksheetSheet.Cells.get_Range("A1", "A1");

cell[1, 1] = "Mobileno";
Excel.Range cellrange = (Excel.Range)cell.get_Resize(20000, 1);
Book.Worksheets.Add(m_oMissing, m_oMissing, m_oMissing, m_oMissing);
cellrange.NumberFormat = "0";
body = (Excel.Range)worksheetSheet.Cells.get_Range("B1", "B1");
body[1, 1] = "StatusDate";
Excel.Range bodyrange = (Excel.Range)body.get_Resize(20000, 1);
bodyrange.NumberFormat = "m/d/yyyy";


}
and
fill data from dataset with this code

OpenReportTempalte();

// Get employee data
DataSet oRptData = new DataSet();
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["sqlcon"]);
con.Open();

string strp = "Select Top 2 * from BillingSMSMessages where ClientId=188";
SqlCommand com = new SqlCommand(strp);
com.CommandText = strp;
com.Connection = con;
com.CommandType = CommandType.Text;
SqlDataAdapter adp = new SqlDataAdapter(com);
adp.Fill(oRptData);
int nRow = 2;


foreach (DataRow oRow in oRptData.Tables[0].Rows)
{
worksheetSheet.Cells[nRow, 1] = oRow["SM_MobileNo"];
worksheetSheet.Cells[nRow, 2] = oRow["SM_StatusDate"];
nRow++;
}
if (File.Exists(vpath) == true)
{
File.Delete(vpath);
worksheetSheet.SaveAs(vpath, m_oMissing, m_oMissing, m_oMissing, m_oMissing, m_oMissing, m_oMissing, m_oMissing, m_oMissing);
sReportFile = vpath;
}
else
{
worksheetSheet.SaveAs(vpath, m_oMissing, m_oMissing, m_oMissing, m_oMissing, m_oMissing, m_oMissing, m_oMissing, m_oMissing);
sReportFile = vpath;
}
System.GC.Collect();



but here is problem when huge data being downloaded like
50000 records and more.can u suggest me how can i export huge data from dataset in excel format and
create multiple sheet also

please suggest me thanking u.

EDIT: Please use code tags when posting your code => code.gif smile.gif

This post has been edited by PsychoCoder: 11 Feb, 2008 - 06:38 AM
User is offlineProfile CardPM
+Quote Post

realwish
RE: Excel Download
26 Feb, 2008 - 03:33 AM
Post #2

New D.I.C Head
Group Icon

Joined: 29 Jan, 2008
Posts: 44


Dream Kudos: 25
My Contributions
PLS SEND THE ZIP CODE FILE TOO
User is offlineProfile CardPM
+Quote Post

Nayana
RE: Excel Download
26 Feb, 2008 - 04:19 AM
Post #3

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
Please DO NOT send the zip code file.
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Excel Download
26 Feb, 2008 - 05:02 AM
Post #4

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,282



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
Your bottleneck is most likely your Excel.Workbooks object. Populating an instance of excel with the data like you're doing can take a very long time, in addition to a lot of cpu. You're probably getting timeout issues.

The best method I've found for this is to write the excel file directly. This isn't particularly hard if you know XML, or are just willing to play with text files. Take a template file from excel, put a couple sample rows in, and save it as an XML spreadsheet. When you look at what you've made in a text editor, you can find your sample rows. e.g.

xml

<Row>
<Cell><Data ss:Type="Number">1</Data></Cell>
<Cell><Data ss:Type="DateTime">2008-01-01T00:00:00.000</Data></Cell>
</Row>


Basically, you can create these for yourself using your data source and writing to a text file. Serve the xml file you create as a text file an you're set.

Anyway, that's one attack.

Other than that, at the least I'd use a SqlCommand rather than the whole adapter. You're loading all the data into memory. You're not really manipulating it, just dumping it out again. You'll see an improvment if you just open a DataReader use a loop.

Hope this helps.

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:10PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month