мой код, вероятно, всегда пытается прочитать файл Excel - PullRequest
0 голосов
/ 08 апреля 2020
    public void ReadExcelFileDOM(string fileName)
    {
        using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(fileName, false))
        {

            WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
            WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
            SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();

            int i = 0;
            int row = 1;
            int cell = 1;

            List<Wsdata> data = new List<Wsdata>();
            decimal dolarkeeper = 0;
            decimal eurokeeper = 0;
            DateTime datekeeper = new DateTime(01/01/2000);



            foreach (Row r in sheetData.Elements<Row>())
            {

                //cellvalue tarihte calismiyo orayi cozduk-alternatif bul
                foreach (Cell c in r.Elements<Cell>())
                {
                    if (c.FirstChild != null && r.FirstChild != null)
                    {

                        if (cell == 1 && row > 1)
                        {
                            //datekeeper = Convert.ToDateTime(c.InnerText);


                            string text = c.CellValue.InnerText;

                            double d = double.Parse(c.InnerText);
                            DateTime conv = DateTime.FromOADate(d).Date;
                            datekeeper = conv;
                            //DateTime.TryParse(c.ToString(),out datekeeper);
                            //burda ben 40binli falan sayilar almistim onu nasil yaptigimi bul 
                            //d = decimal.Parse(c.CellValue.Text);
                            //d = double.Parse(c.CellValue.Text);
                            //datekeeper = DateTime.FromOADate(double.Parse(c.CellValue.Text));
                            //DateTime.TryParseExact(c.CellValue.Text,"dd/MM/yyyy",TR,DateTimeStyles.None,out datekeeper);
                            //datekeeper = DateTime.Parse(c.CellValue.Text);
                        }
                        else if (cell == 2 && row > 1)
                        {//dolar 
                            dolarkeeper = decimal.Parse(c.CellValue.Text);
                        }
                        else if (cell == 3 && row > 1)
                        {//euro 
                            eurokeeper = decimal.Parse(c.CellValue.Text);
                        }
                    }
                    cell++;
                }
                i++;
                cell = 1;
                row++;
                if (i > 1) {
                    data.Add(new Wsdata(dolarkeeper,eurokeeper,datekeeper)
                    {
                        Dolar = dolarkeeper,
                        Euro  = eurokeeper,
                        Dates = datekeeper
                    });
                }
            }
        }    
        Console.WriteLine();
        Console.ReadKey();
    }

Я не понимаю, что после того, как я запустил код, похоже, что я всегда хочу его запустить. Экран консоли всегда остается на экране и использует процессор. Кто-нибудь помогает мне с этим. Нужно ли мне некоторые коды для закрытия Excel в методе "DOM" или я сделал что-то не так. Большое спасибо.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...