Force EPPLUS to read as text

社会主义新天地 提交于 2019-12-01 14:39:48

问题


I'm developping an application to read xlsx files, do some validation and insert into database. Unfortunatelly when I try to read columns marked as numeric (fe with EAN-13 codes) I get miniumum value of an int. The user doesn't see this because Excel displays it properly.

How can I make it read the file as plain text? I know I can use OLEBD for it, but I also need to edit the file dynamically, so epplus ExcelPackage is the best choice.

Here is code im using:

 FileInfo file = new FileInfo(path);
 MainExcel = new OfficeOpenXml.ExcelPackage(file);
 {
   var ws = MainExcel.Workbook.Worksheets.First();
   DataTable tbl = new DataTable();
        for (var rowNum = 1; rowNum <= ws.Dimension.End.Row; rowNum++)      //currently loading all file
                {
                    var wsRow = ws.Cells[rowNum, 1, rowNum, ws.Dimension.End.Column];
                    var row = tbl.NewRow();
                    foreach (var cell in wsRow)
                    {
                        row[cell.Start.Column - 1] = cell.Text;
                    }
                    tbl.Rows.Add(row);
                }          
 }

and that's how I enumerate columns

   foreach (var firstRowCell in ws.Cells[3, 1, 3, ws.Dimension.End.Column])
                {
                    System.Type typeString = System.Type.GetType("System.String") ;
                    tbl.Columns.Add( firstRowCell.Text , typeString );
                }

For people whom it might concern, here is the file (works also for non google users): https://drive.google.com/open?id=0B3kIzUcpOx-iMC1iY0VoLS1kU3M&authuser=0

I noticed that ExcelRange.value property is an array which contains all of the objects unformatted. But once you iterate over cells in ExcelRange and request cell.Text property, it has already been processed. Trying to modify ConditionalFormatting and DataValidation in ExcelRange does not help (f.e. AddContainsText()) - @EDIT--> Neither for an entire sheet :-(

I'd prefer NOT to cast ExcelRange.Value as Array, it's ugly and very conditional.


回答1:


Apparently this is the solution (not complete code though, you have to add columns to datatable). I couldn't find the format string which specifies 'no formatting' in Epplus, but here you have it.

 var ws = MainExcel.Workbook.Worksheets.First();
 DataTable tbl = new DataTable();
 for (var rowNum = 1; rowNum <= ws.Dimension.End.Row; rowNum++)      
 {
     var wsRow = ws.Cells[rowNum, 1, rowNum, ws.Dimension.End.Column];
     var array = wsRow.Value as object[,];

     var row = tbl.NewRow();
     int hhh =0;

     foreach (var cell in wsRow)
          {
           cell.Style.Numberformat.Format = "@";
           row[cell.Start.Column - 1] = cell.Text;
          }
     tbl.Rows.Add(row);
 }



回答2:


The cells in your file are custom-formatted as a fraction. Have you done this on purpose? Anyway, if you want to keep this format, you can alternatively use cell.Value or cell.RichText.Text to get your 13-digit number.

Hope this helps.



来源:https://stackoverflow.com/questions/29429797/force-epplus-to-read-as-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!