0

I have a project that read an excel file and write into some data later on download this file. I have created this project in ASP.NET.

I can read excel file and write into something then I save it as different name.In my local computer this code work properly and I do not have any error in these code.But When I publish this project I did not get any error but my project did not give me any result of as I wanted. How can I solve this problem?

 public static void ReadExistingExcel()
        {
            #region excelformac
           // var fileName = "SOF46501-firmaadi-08062015.xltx"; 
            string path = HttpContext.Current.Server.MapPath("~/File/SOF46501-firmaadi-08062015.xltx");

            // path = "D:\\SOF46501-firmaadi-08062015.xltx";
            oXL = new Microsoft.Office.Interop.Excel.Application();
            oXL.Visible = true;
            oXL.DisplayAlerts = false;
            mWorkBook = oXL.Workbooks.Open(path, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
            //Get all the sheets in the workbook
            mWorkSheets = mWorkBook.Worksheets;
            //Get the allready exists sheet
            mWSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)mWorkSheets.get_Item("Standart USD");
            Microsoft.Office.Interop.Excel.Range range = mWSheet1.UsedRange;
            #endregion
            int sofNo = 0, sofNo2 = 0;
            ProductBUS productBUS = new ProductBUS();
            List<UrunCikisVO> uruncikisVO = new List<UrunCikisVO>();
            uruncikisVO = productBUS.GetProduct();
            sofNo = productBUS.SofNo();
            sofNo2 = sofNo + 1;
            if (productBUS.SofUpdate(sofNo2))
            {

            }
            int j = 0;
            for (int i = 0; i < uruncikisVO.Count; i++)
            {
                if (i % 10 == 0)
                {
                    j = 0;
                    sofNo = productBUS.SofNo();
                    sofNo2 = sofNo + 1;
                    if (productBUS.SofUpdate(sofNo2))
                    {

                    }
                }
                else
                {
                    j++;
                }
                mWSheet1.Cells[i + 8, 2] = uruncikisVO[i].UrunKodu;
                mWSheet1.Cells[i + 8, 4] = uruncikisVO[i].UrunTanimi;
                mWSheet1.Cells[i + 8, 5] = uruncikisVO[i].Adet;
                mWSheet1.Cells[i + 8, 6] = uruncikisVO[i].BirimFiyat;
                mWSheet1.Cells[i + 8, 7] = uruncikisVO[i].ToplamFiyat;
                mWSheet1.Cells[2, 3] = sofNo;
                mWSheet1.Cells[3, 3] = uruncikisVO[0].SiparisTarihi;
                mWSheet1.Cells[4, 3] = uruncikisVO[0].ResmiAdi;
                mWSheet1.Cells[5, 3] = uruncikisVO[0].phone;
                mWSheet1.Cells[6, 7] = uruncikisVO[0].ParaBirim;
                mWSheet1.Cells[20, 4] = uruncikisVO[0].MüsteriTemsilcisi;
                mWSheet1.Cells[21, 4] = uruncikisVO[0].Notlar;
                mWSheet1.Cells[22, 4] = uruncikisVO[0].SiparisTarihi + "/" + uruncikisVO[0].OdemeSekli;
                mWSheet1.Cells[23, 4] = uruncikisVO[0].TeslimatTarihi;
                mWSheet1.Cells[24, 4] = uruncikisVO[0].VergiDairesi + " / " + uruncikisVO[0].VergiNo;
                mWSheet1.Cells[25, 4] = uruncikisVO[0].ResmiAdi;
                mWSheet1.Cells[26, 4] = uruncikisVO[0].FirmaAdresi;
            }
            #region excelsave_Close
            mWorkBook.SaveAs("D:\\SOF" + sofNo + "-" + uruncikisVO[0].ResmiAdi + "-" + DateTime.Now.ToShortDateString() + ".xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            mWorkBook.Close(Type.Missing, Type.Missing, Type.Missing);
            mWSheet1 = null;
            mWorkBook = null;
            oXL.Quit();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            #endregion
        }
Ullas
  • 11,450
  • 4
  • 33
  • 50
Diego
  • 937
  • 8
  • 24
  • Where are you stuck? Read a file, downloading a file, etc.? – David Jun 30 '15 at 14:42
  • In my local computer I can read and write ect – Diego Jun 30 '15 at 14:51
  • But When publish my project I cant read file and others – Diego Jun 30 '15 at 14:51
  • 1
    Why can't you? What's stopping you? If you have specific code and a specific error that would be helpful. – David Jun 30 '15 at 14:53
  • What indication do you have that this isn't working? Add some logging or other output for production debugging. Also, this may be obvious but it's surprising how often it happens... You *do* know that the file would be saved on the *server* and not in your computer, right? – David Jul 01 '15 at 09:40

1 Answers1

2

It might actually be the version of Microsoft used on Local vs on Production. I'd suggest you look into something like EPPlus http://epplus.codeplex.com/. This will give you the flex of using the right DLL both locally and on production.

More references
Create/Read/Edit Advance Excel 2007/2010 Report in C#.Net using EPPlus
Writing Excel Files using EPPLus
Import/Export Data from Excel Files (.xlsx) to DataGridView or Vice Versa Using EPPlus
and check this answer as well
Reading Excel spreasheet using EPPlus

Community
  • 1
  • 1
RealSollyM
  • 1,530
  • 1
  • 22
  • 35
  • Which dll I must add my project? – Diego Jul 01 '15 at 07:39
  • EPPlus.dll - you can also get this from NuGet. – RealSollyM Jul 01 '15 at 09:10
  • What is the problem this time? Follow these links to resolve your issue? I have added more references to the answer. – RealSollyM Jul 01 '15 at 12:57
  • oXL = new Microsoft.Office.Interop.Excel.Application(); I had tried your method but I could not some my problem. Do you know any other solution for solving problem? I got first error in this code: oXL = new Microsoft.Office.Interop.Excel.Application(); – Diego Jul 02 '15 at 08:22
  • You are still using Microsoft.Office namespace. If you say EPPlus doesn't work, then show what you did and I can help you with that one. – RealSollyM Jul 02 '15 at 09:08
  • your method is different, and I used it but it is overwrite exist file as an new file and it is useless method, do you know another method? – Diego Jul 24 '15 at 08:54
  • I use the same export method and it doesn't overwrite any files. If FileExport.xlsx exists, it will save it as FileExport(1).xlsx and so forth. You probably specified to overwrite somewhere. If you were to show code, I'd be able to help. – RealSollyM Jul 29 '15 at 07:01