简体   繁体   English

在Excel工作表中加载tsv / csv文件

[英]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. 之后,我调用LoadSheet方法以从tsv文件填充我的excel文件。

    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. 一切正常,我创建了一个新的Excel文件,将其保存,但它不会加载我需要在excel文件中加载的数据。 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. 谢谢大家。

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 问题是文件中的Unix风格的换行符-EPPlus默认情况下期望Windows风格的换行符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM