Home>

I want to open Excel using CLosedXML, but I cannot open it because the path does not pass. Please tell me what is missing.

Error message
System.FormatException: 'The format of the input string is not correct. '
Applicable source code
var path = @ "\ Users \ ****** \ source \ repos \ testapp \ testapp \ bin \ Debug \ ****. xlsx";
// read Excel file
using (var book = new XLWorkbook (path, XLEventTracking.Disabled)) ← An error occurs here.
{
var sheet = book.Worksheet ("Shito");
sheet.Cell ("A1"). Value = "Value";
book.Save ();
}
Tried

var path = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.Desktop),"****. xlsx");

var path = Path.GetFullPath (".. \\ ****. xlsx");

Uri u1 = new Uri ("C: \\ Users \\***\\ Desktop \\");
Uri u2 = new Uri (u1,".. \\ ****. Xlsx");
var path = u2.LocalPath;

I have tried these codes that I have examined.
However, both gave an error.

Additional
@"\ Users \**\ source \ repos \ testapp \ testapp \ bin \ Debug \**. xlsx" ;; This part @"C: \ Users \ ****\ source \ repos \ testapp \ testapp \ bin \ Debug \**. xlsx" ;; And change System.IO.DirectoryNotFoundException:'path'c: \ users \ ****\ source \ repos \ testapp \ testapp \ bin \ Debug \ Users \**\ source A part of \ repos \ testapp \ testapp \ bin \ Debug \ ****. xlsx'was not found.'
Error.

Additional
var path = @"******. xlsx" ;;
String filename = path;
if (File.Exists (filename))
{
MessageBox.Show ("exists");
}
else
{
MessageBox.Show ("Not present");
}
When this code is executed, "exists" is displayed.
So there may still be problems outside the code.

Additional
Results with OpenXML
var filename = @"C: \ Users \ ****** \ source \ repos \ testapp \ testapp \ bin \ Debug \ ******. xlsx" ;;
using (SpreadsheetDocument document = SpreadsheetDocument.Open (filename, false))
{
MessageBox.Show ("Open");
}
When you execute this, "Open" is displayed as the execution result.

Additional
I created a new Excel file and saved it as empty, and then opened it with ClosedXML.

Supplemental information (FW/tool version etc.)

VisualStudio 2017
ClosedXML 0.93.1
Excel for Ofiice 365 MSO (16.0.10730.20053) 32-bit

  • Answer # 1

    The file exists

    It works in papinianus' environment

    If

    , lable's environment-dependent problem is likely.
    You should first verify that the xlsx you are trying to process is correct.

    Not mentioned in the question, how is this xlsx file created?

    Check that it can be opened normally with EXCEL (version?)

    Check if it works when replaced with another xlsx file

    can be considered.
    Since the corresponding part of ClosedXml seems to wrap OpenXml as it is,
    I think that it is also good to check whether it can be opened with OpenXml attached tool (there should have been a verification tool).

  • Answer # 2

      

    var path = @"\ Users \**\ source \ repos \ testapp \ testapp \ bin \ Debug \ ****. xlsx" ;;

    Full-width spaces are confused
    What about the OS?
    In Windows, the path format is different.

  • Answer # 3

    We have tested ClosedXML now, but Windows recognizes both absolute and relative paths.

    I think there is a problem with the duplicated paths, as you can see in the comment section of another answer.
    I think that only happens in the code (no problem outside the code).

    If you have written it exactly as you asked, and modified it as described, I don't think it will work. Please make sure++ =is not used.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using ClosedXML.Excel;
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main (string [] args)
            {
                var path = @ "book.xlsx";
                // var path = @ "C: \ .. Desktop .. \ ConsoleApp1 \ ConsoleApp1 \ bin \ Debug \ book.xlsx";
                // read Excel file
                using (var book = new XLWorkbook (path, XLEventTracking.Disabled))
                {
                    var sheet = book.Worksheet ("Shito");
                    sheet.Cell ("A1"). Value = "Value";
                    book.Save ();
                }
            }
        }
    }

  • Answer # 4

    It's not self-solving, but write down the process of resolution.
    I wrote in the answer of the best answer,
    The Excel file I was running first seemed to have no problem with the extension .xlsx, but in fact it was
    It was called Strict Open XML Spreadsheet (.xlsx). It is unclear why this extension was used. (Maybe you did it yourself)
    So,
    If you save the file with an extension of Excel book (.xlsx), you can open it with ClosedXML.

    The solution is
    Thanks to mituha, y_waiwai and papinianus.
    I'm really thankful to you.