2

I m having function which export data of dataset to an excel sheet. it is working fine on local machine but whn i upload ths code to server it not work ....

code behind file:

    using Excel = Microsoft.Office.Interop.Excel;
    using System.Reflection;
    using System.Drawing;
    using System.ComponentModel;
    using System.Windows.Forms;

     string FilePath = ConfigurationManager.AppSettings["dataqueryfile"]
                 + "dataquery_" + DateTime.Now.ToString("yyyyMMddhhmm") ;


                Excel.Application oXL;
                Excel.Workbook oWB;
                Excel.Worksheet oSheet;
                Excel.Range oRange;

                // Start Excel and get Application object. 
                oXL = new Excel.Application();

                // Set some properties 
                oXL.Visible = true;
                oXL.DisplayAlerts = false;

                // Get a new workbook. 
                oWB = oXL.Workbooks.Add(Missing.Value);


                // Get the active sheet 
                oSheet = (Excel.Worksheet)oWB.ActiveSheet;
                oSheet.Name = "DataQuery";

                // Process the DataTable 
                DataTable dt = ds.Tables[0];

                int rowCount = 1;
                foreach (DataRow dr in dt.Rows)
                {
                    rowCount += 1;
                    for (int i = 1; i < dt.Columns.Count + 1; i++)
                    {
                        // Add the header the first time through 
                        if (rowCount == 2)
                        {
                            oSheet.Cells[1, i] = dt.Columns[i - 1].ColumnName;
                        }
                        oSheet.Cells[rowCount, i] = dr[i - 1].ToString();
                    }
                }

                // Resize the columns 
                oRange = oSheet.get_Range(oSheet.Cells[1, 1],
                              oSheet.Cells[rowCount, dt.Columns.Count]);
                oRange.EntireColumn.AutoFit();

                // Save the sheet and close 
                oSheet = null;
                oRange = null;


                oWB.SaveAs(FilePath + ".xls", Excel.XlFileFormat.xlWorkbookNormal,
                    Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                    Excel.XlSaveAsAccessMode.xlExclusive,
                    Missing.Value, Missing.Value, Missing.Value,
                    Missing.Value, Missing.Value);
                oWB.Close(Missing.Value, Missing.Value, Missing.Value);
                oWB = null;
                oXL.Quit();

                // Clean up 
                // NOTE: When in release mode, this does the trick 
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();

web config file :

      <add key="dataqueryfile" value="C:\navin\"/>
      <add assembly="Microsoft.Office.Interop.Excel, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"/>
  <add assembly="office, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"/>
            <add assembly="Microsoft.Vbe.Interop, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"/>
            <add assembly="stdole, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>

edited :

                da = new SqlDataAdapter();
                conn.Open();
                da.SelectCommand = command;
                da.Fill(ds);
                ds.WriteXml("c:\\Customers.xml");

when i execute solution from the local system the data set values are copied to the xml file along with column header..then i moved the files to virtual directory under inetpub folder. through iis manager i browsed the virtual directory and run the same page , the xml file is getting saved in the same path but without the column header :(.. then i opened the same page from other machine through URL (thru inetrnal ip address) but the xml file is not saving to the machine.. plz help me out..

thanx n regards

T.Navin

navbingo
  • 223
  • 1
  • 6
  • 24
  • You forgot to post the error messages/stack traces/log file entries that you are receiving when the application "not work". – mdm May 26 '11 at 10:11
  • i am not getting any error message...the file is not getting downloaded in the specified folder... when i run from the local machine its getting saved in the folder.. – navbingo May 26 '11 at 10:15
  • Unless you can provide some information like an error message or stack trace, I don't think you are going to get any useful answers. – mdm May 26 '11 at 10:21
  • @mdm - we had the similar problems - no exceptions, no error logs, nothing - just the file was missing... so I think the question is valid.. – veljkoz May 26 '11 at 10:25
  • @veljkoz how did u over come the issue.. plz help me out – navbingo May 26 '11 at 10:31
  • @navbingo - I've already put that in my answer - it was configuration issue. Also, if it's an option - plug in your phones to the server - if you hear the window popup sound when the excel is automated it means that Excel shows some dialog window and is waiting for answer but you can't see it - you'll need to guess what is he asking and change your automation to work around it... but, as I've said in my answer, it's really better to create excel files manually without involving any Office instalation at all... – veljkoz May 26 '11 at 10:42
  • as you suggested, i am working on it.. just now transferred the data set values to XML file.. now i have to transfer it back to csv or xls format. – navbingo May 26 '11 at 11:10
  • You might find that more difficult than you would expect (believe me). Why does it have to be in xls? XML has instruction in it's first line that specifies which program to open with (in this case Excel) so it's really OK to leave it in XML from users point of view (it's fully compatible with all that Excel has) – veljkoz May 26 '11 at 14:21
  • @veljkoz..k thanks , i will leave it in xml itself.. – navbingo May 27 '11 at 02:45
  • see [sample to export DataSet to Excel](http://stackoverflow.com/q/373925/277290) using xml. Possible duplicate. – YetAnotherUser May 27 '11 at 03:36
  • @yetanotheruser: still facing the same issue :( – navbingo May 27 '11 at 04:50

1 Answers1

1

Automating Word or Excel on services is strongly discouraged, but you can read some considerations about it here. First of all you could try setting your service to run as Local System. Other thing to play with is to find the Excel in the:

Control Panel - Administrative Tools - Component services - Component services - My computer - DCOM Config

There you can set the priviledges under which the Excel can run, etc...

But, I have to say, even with everything setup properly it can still turn out not working. We had intense difficulties with this, with some server working, and the exact same configuration not working on some other server...

So, what I would suggest (since your need doesn't really require automation) is to create Excel files yourself using XSLT to create SpreadSheetML - it's actually more easy than you'll think at first, but it's really a goood way to go. Try saving your XLS file in an Office XML 2003 format, and see the structure of the XML - if you look at the body it's really not that complicated. When you create these XML's the Excel can open it just like any other file.

Another way (if you don't like using XSL Transformations) is to use Open XML Format SDK - we found that one to be a bit tedious to implement for Word (we needed some nuances) so we still prefer the XSLT's, but for Excel it could prove to be quite easy. With this SDK you can even save as XSLX/DOCX formats (which is actually very similar to ProcessingML, but packed into a zip)

Hope it helps.

veljkoz
  • 8,384
  • 8
  • 55
  • 91
  • da.Fill(ds); ds.WriteXml("c:\\Customers.xml"); when i execute solution from the local system the data set values are copied to the xml file along with column header..then i moved the files to virtual directory under inetpub folder. through iis manager i browsed the virtual directory and run the same page , the xml file is getting saved in the same path but without the column header . then i opened the same page from other machine through URL but the xml file is not saving to the machine.. plz help me out. – navbingo May 27 '11 at 03:26
  • This looks like a permission issue - with which account does your application pool runs with? What IIS version? – veljkoz May 27 '11 at 07:51
  • Have you set the credentials with which that application pool runs to be Local System instead of Network Service? – veljkoz May 27 '11 at 09:23
  • i have set the application pool pre defined to network service...i will change to local system now..and thn run my program and then i will let you know.. – navbingo May 27 '11 at 09:31
  • @veljkoz, as you suggested i have changed the settings..but still facing the same issue..its not getting downloaded in local system – navbingo May 27 '11 at 09:38
  • Saving as XML doesn't require any automation which is a typical problem with this (it doesn't even require Office instalation), only the File Write permissions. So, the problem is definitely with permissions - try to work something out with it, make sure you've setup the service to run with your pool, the pool is running as local system, that the system has write permissions to the folder, etc... – veljkoz May 27 '11 at 11:08