Esempio n. 1
0
 public static int fReadInforFromFileName(string strFilename, Packagelist oPackagelist)
 {
     try
     {
         string[] strInfo     = strFilename.Split("-");
         string[] strDP       = strInfo[6].Split(" ");
         string[] strProvince = strInfo[8].Split(" ");
         oPackagelist.PO_number = strDP[1];
         oPackagelist.Province  = strProvince[0];
         oPackagelist.Year      = strInfo[1] + strInfo[2];
         return(1);
     }
     catch (Exception)
     {
         return(0);
     }
 }
Esempio n. 2
0
        public static int fReadPackageListFiles(string[] fnames, ref List <Packagelist> oPackagelist)
        {
            ExcelOffice.Application     xlApp          = null;
            List <ExcelOffice.Workbook> xlWorkbookList = null;

            ExcelOffice.Range      xlRange     = null;
            ExcelOffice.Workbook   xlWorkbook  = null;
            ExcelOffice._Worksheet xlWorksheet = null;
            List <string>          ListSerial  = new List <string>();

            try
            {
                xlApp          = new ExcelOffice.Application();
                xlWorkbookList = new List <ExcelOffice.Workbook>();
                foreach (string strfilename in fnames)
                {
                    int i = 0;
                    /*Read infor from file name*/
                    //int iRet = fReadInforFromFileName(strfilename, oPackagelist[i]);
                    //ExcelOffice.Application xlApp1;
                    //ExcelOffice.Workbook xlWorkBook1;
                    //ExcelOffice.Worksheet xlWorkSheet1;
                    //object misValue = System.Reflection.Missing.Value;

                    //xlApp1 = new ExcelOffice.Application();
                    //xlWorkBook1 = xlApp.Workbooks.Open(strfilename, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                    //xlWorkSheet1 = (ExcelOffice.Worksheet)xlWorkBook1.Worksheets.Item[1];

                    //MessageBox.Show(xlWorkSheet1.get_Range("A1", "A1").Value2.ToString());

                    //xlWorkBook1.Close(true, misValue, misValue);
                    //xlApp1.Quit();

                    //releaseObject(xlWorkSheet1);
                    //releaseObject(xlWorkBook1);
                    //releaseObject(xlApp1);


                    xlWorkbook = xlApp.Workbooks.Open(strfilename);
                    //xlWorkbook = xlApp.Workbooks.Open(strfilename, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                    xlWorksheet = (ExcelOffice._Worksheet)xlWorkbook.Sheets[3];
                    xlRange     = xlWorksheet.UsedRange;
                    string xName    = xlWorksheet.Name.ToString();
                    int    rowCount = xlRange.Rows.Count;
                    int    colCount = xlRange.Columns.Count;
                    /*Get conntet Danh Sach Tinh/Tp*/
                    for (int j = 2; j <= rowCount; j++)
                    {
                        //string strCase = Convert.ToString((xlRange.Cells[i, 1] as ExcelOffice.Range).Text);
                        //string strStorage = Convert.ToString((xlRange.Cells[i, 3] as ExcelOffice.Range).Text);
                        string strSerial = Convert.ToString((xlRange.Cells[j, 3] as ExcelOffice.Range).Text);
                        //string strMAC = Convert.ToString((xlRange.Cells[i, 3] as ExcelOffice.Range).Text);
                        //string strSerial_gpon = Convert.ToString((xlRange.Cells[i, 3] as ExcelOffice.Range).Text);
                        if (string.Empty != strSerial)
                        {
                            Packagelist temp = new Packagelist();
                            temp.SetSerial(strSerial);
                            int ret = fReadInforFromFileName(strfilename, temp);
                            oPackagelist.Add(temp);
                        }
                        else
                        {
                            j = rowCount + 1;
                        }
                    }
                    i++;
                }


                //cleanup
                GC.Collect();
                GC.WaitForPendingFinalizers();
                //rule of thumb for releasing com objects:
                //  never use two dots, all COM objects must be referenced and released individually
                //  ex: [somthing].[something].[something] is bad

                //release com objects to fully kill excel process from running in the background
                Marshal.ReleaseComObject(xlRange);
                Marshal.ReleaseComObject(xlWorksheet);

                //close and release
                xlWorkbook.Close();
                Marshal.ReleaseComObject(xlWorkbook);

                //quit and release
                xlApp.Quit();
                Marshal.ReleaseComObject(xlApp);
                return(1);
            }
            catch (Exception)
            {
                //cleanup
                GC.Collect();
                GC.WaitForPendingFinalizers();
                //rule of thumb for releasing com objects:
                //  never use two dots, all COM objects must be referenced and released individually
                //  ex: [somthing].[something].[something] is bad

                //release com objects to fully kill excel process from running in the background
                Marshal.ReleaseComObject(xlRange);
                Marshal.ReleaseComObject(xlWorksheet);

                //close and release
                xlWorkbook.Close();
                Marshal.ReleaseComObject(xlWorkbook);

                //quit and release
                xlApp.Quit();
                Marshal.ReleaseComObject(xlApp);
                return(0);
            }
        }