Error: Unrecognized escape Quence

Asked

Viewed 37 times

1

I am developing a program in c# to read files. xlsx and it is giving error in the path "path" on line 9 of that file and I don’t know why.

static void Main(string[] args)
    {
       static System.Data.DataSet GetExcelData(string Workbook)
        {
            DataSet SeleniumElementSet = new DataSet();
            var excelApp = new Microsoft.Office.Interop.Excel.Application();
            var datatable = new System.Data.DataTable();
            string[] strrow = new string[2];
            string caminho = "C:Desktop\Dataset.xlsx";

            int rCnt = 0;
            int cCnt = 0;
            Microsoft.Office.Interop.Excel.Application xlApp;
            Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
            Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
            Microsoft.Office.Interop.Excel.Range range;

            xlApp = new Microsoft.Office.Interop.Excel.Application();
            xlWorkBook = xlApp.Workbooks.Open((AppDomain.CurrentDomain.BaseDirectory + caminho + Workbook),
            0,true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, false, 1, 0);
            int worksheetcount = xlWorkBook.Worksheets.Count;
            //int wks = 1;
            for (int wks = 1; wks <= worksheetcount; wks++)
            {
                xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(wks);
                String datatablename = xlWorkSheet.Name;
                range = xlWorkSheet.UsedRange;
                //Console.WriteLine(wks);

                var dt = new System.Data.DataTable(datatablename);

                int rowCount = range.Rows.Count;
                int colCount = range.Columns.Count;
                //int colCount = 3;
                string[] headers = new string[colCount];
                for (cCnt = 1; cCnt <= colCount; cCnt++)
                {

                    headers[cCnt - 1] = (Convert.ToString((range.Cells[1, cCnt] as Microsoft.Office.Interop.Excel.Range).Value2));
                    dt.Columns.Add(headers[cCnt - 1]);
                    //Console.WriteLine(headers[cCnt - 1]);
                }
                for (rCnt = 2; rCnt <= rowCount; rCnt++)
                {
                    var array = new object[colCount];

                    for (cCnt = 1; cCnt <= colCount; cCnt++)
                    {
                        array[cCnt - 1] = (Convert.ToString((range.Cells[rCnt, cCnt] as Microsoft.Office.Interop.Excel.Range).Value2));

                    }

                    dt.Rows.Add(array);


                }
                SeleniumElementSet.Tables.Add(dt);
            }
            xlApp.Quit();

            return SeleniumElementSet;
        }
    }
  • puts @"C:\Desktop\Dataset.xlsx" or "C:\\Desktop\\Dataset.xlsx"

  • What a mistake you’re making, buddy?

1 answer

0


In the file path is missing a bar after two points and you need to 'escape' the bars, can do this in two ways:

string caminho = @"C:\Desktop\Dataset.xlsx";

or

string caminho = "C:\\Desktop\\Dataset.xlsx";
  • found in English: https://stackoverflow.com/questions/1302864/unrecognized-escape-sequence-for-path-string-containing-backslashes

  • Removing the comment right away because now it’s right ...

  • good. straight, vlw friend

Browser other questions tagged

You are not signed in. Login or sign up in order to post.