Click here to Skip to main content
15,881,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all of you !

I am suffering from a crucial problem here. I have a project in which we give users functionality to eider download or email the data in .xls format. The .xls sheet is highly modified using watermarks, cell widths, etc etc. The problem is the code is running amazingly on local host but not on live. I tried removing try-catch to se wats the error still I am not even getting any error. When I go for download it starts downloading and then stops in between. I save the file first on server in the folder name downloads which has full permissions. I first write the sheet from datatable to xls. than using com objects of interop excel I pick this file maodify it save it with a diff name in the same folder and than delete the older one. This is how It all works. But all in vain on live. Plz I need serious help. This is about to gulp my job today. I am really tensed. I have tried everything. here is some code that I am using :
C#
private static string Recreating_Ecxel_WorkSheet(string strWorkOrder, string strDwnload_file_name)
    {
        // Creating Objects.
        object oMissing = System.Reflection.Missing.Value;
        Excel.ApplicationClass xl = new Microsoft.Office.Interop.Excel.ApplicationClass();
        Excel.Workbook xlBook;
        Excel.Worksheet xlSheet;
        Excel.Range range;

        //Accessing Sheet and Image for insertion.
        string laPath = HttpContext.Current.Server.MapPath("~//download//" + strDwnload_file_name + ".xls");
        xlBook = (Workbook)xl.Workbooks.Open(laPath, oMissing,
        oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
        xlSheet = (Worksheet)xlBook.Worksheets.get_Item(1);
        xlSheet.Cells.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
        xlSheet.SetBackgroundPicture(HttpContext.Current.Server.MapPath("~//images//cybex-punchline_half.jpg"));
        xlSheet.Cells.WrapText = false;

        //range = xlSheet.get_Range("A:A", System.Type.Missing);
        //range.EntireColumn.ColumnWidth = 15;
        xlBook.Worksheets.get_Item(1);
        ((Range)xlSheet.Columns["A", Type.Missing]).ColumnWidth = 13;
        ((Range)xlSheet.Columns["B", Type.Missing]).ColumnWidth = 09;
        ((Range)xlSheet.Columns["C", Type.Missing]).ColumnWidth = 65;
        ((Range)xlSheet.Columns["D", Type.Missing]).ColumnWidth = 13;
        ((Range)xlSheet.Columns["E", Type.Missing]).ColumnWidth = 10;
        ((Range)xlSheet.Columns["F", Type.Missing]).ColumnWidth = 22;
        ((Range)xlSheet.Columns["G", Type.Missing]).ColumnWidth = 10;
        ((Range)xlSheet.Columns["H", Type.Missing]).ColumnWidth = 30;
        ((Range)xlSheet.Columns["I", Type.Missing]).ColumnWidth = 12;
        ((Range)xlSheet.Columns["J", Type.Missing]).ColumnWidth = 25;


        string str_modified_file_download_name = dwnlod_file_name(strWorkOrder);
        // Saving File on disk.
        xlBook.SaveAs(HttpContext.Current.Server.MapPath("~//download//" + str_modified_file_download_name + ".xls"), XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing,
        false, false, XlSaveAsAccessMode.xlNoChange,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

        //xlBook.Sheets("str").Cells.WrapText = False;


        xl.Application.Workbooks.Close();
        FileInfo fi1 = new FileInfo(laPath);
        if (fi1.Exists)
            fi1.Delete();

        // Releasing Objects.
        releaseObject(xlBook);
        releaseObject(xlSheet);
        releaseObject(xl);
        return str_modified_file_download_name;
    }

Pls does anyone know something about it ? Please help me
Posted
Comments
darshan_ur 4-Sep-12 7:12am    
Hi Taresh a simple question did you check the server for the xls file which you said that should be created.
I am not sure but i think the issue might be with the IIS user account as it might not have the priviledge to write onto the folder.
Taresh Uppal 4-Sep-12 7:19am    
darshan..I have already given full controls from all accounts...

1 solution

Office interop libraries require you to install Office on your server machine.

This is generally a bad idea, it is better and more performant to use specific libraries like http://epplus.codeplex.com/[^] for XLS(X) file processing which do not require anything to be installed and work on servers.
 
Share this answer
 
Comments
Taresh Uppal 4-Sep-12 7:02am    
Sir, On my server microsft office 2007 is already installed.
AmitGajjar 4-Sep-12 7:31am    
Really good link 5+
Mehdi Gholam 4-Sep-12 7:35am    
Thanks Amit!
Taresh Uppal 4-Sep-12 7:38am    
any other idea ???
Bernhard Hiller 4-Sep-12 7:47am    
very interesting project!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900