Nemanja
Nemanja

Reputation: 73

Loading tsv/csv file in excel sheet

This is my code.

    public static string LoadPackage(DirectoryInfo outputDir, string name)
    {
        FileInfo newFile = new FileInfo(outputDir.FullName + @"\test.xlsx");
        if (newFile.Exists)
        {
            newFile.Delete();
            newFile = new FileInfo(outputDir.FullName + @"\test.xlsx");
        }

        var format = new ExcelTextFormat();
        format.Delimiter = '\t';
        format.SkipLinesBeginning = 1;

        using (ExcelPackage package = new ExcelPackage())
        {
            LoadSheet(package, outputDir, name);

            package.SaveAs(newFile);

        }
        return newFile.FullName;
    }

And after that i call LoadSheet method in order to fill my excel file from tsv file.

    public static void LoadSheet(ExcelPackage package, DirectoryInfo 
    outputDir, string name)
    {

        var ws = package.Workbook.Worksheets.Add("Content");

        var format = new ExcelTextFormat();
        format.Delimiter = '\t';
        format.SkipLinesBeginning = 2;
        format.SkipLinesEnd = 1;

        var range = ws.Cells["A1"].LoadFromText(new 
        FileInfo(outputDir.FullName + "\\" + name), format, 
        TableStyles.Medium27, false);
    }

And this is my code on button click event

        if (BrowseFileUpload.HasFile)
        {
            var name = BrowseFileUpload.PostedFile.FileName;
            InputTextBox.Text = name;
            LoadData.LoadPackage(new 
            System.IO.DirectoryInfo("C:\\Users\\Nemanja\\Downloads"), name);

            InfoLabel.Text = "Your data has been imported!!!";
            InfoLabel.ForeColor = System.Drawing.Color.Blue;
            InfoLabel.Font.Size = 20;
        }

Everything is ok i create new excel file, sheet save it but it does not load data that i need it to load inside excel file. It's only empty file or i get a error the file is corrupted recover what you can.

Can someone figure out what can be a problem based on my explanation and this code. Thank you all good people.

Upvotes: 0

Views: 479

Answers (1)

Pete
Pete

Reputation: 1867

I think that the problem may well be with the format of your source data. I've put together the following sample, based on your code, and it works fine.

var outFile = Path.ChangeExtension(filePath, ".xlsx");
using (var p = new ExcelPackage())
{
    var fmt = new ExcelTextFormat();
    fmt.Delimiter = '\t';
    fmt.SkipLinesBeginning = 2;
    fmt.SkipLinesEnd = 1;
    fmt.EOL = ((char)10).ToString();   // THIS LINE FIXED THE PROBLEM (UNIX NEWLINE) 

    var ws = p.Workbook.Worksheets.Add("Imported Text");
    ws.Cells[1, 1].LoadFromText(new FileInfo(filePath), fmt, TableStyles.Medium27, false);
    p.SaveAs(new FileInfo(outFile));
}

Try running your data through this and see if you get the same issue or not.

UPDATED

The problem was a unix-style newline in the file - EPPlus expects a windows-style newline by default

Upvotes: 2

Related Questions