本文介绍了力EPPLUS阅读文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的深化发展应用读取XLSX文件,做一些验证和插入到数据库中。可惜的是,当我尝试读取标记为数字列(FE与EAN-13码),我得到一个int的miniumum价值。
用户并不认为这是因为Excel正确地显示出来。

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.

我怎样才能使它读取该文件以纯文本?我知道,我可以用OLEBD它,但我也需要动态编辑文件,所以epplus ExcelPackage是最好的选择。

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

我注意到,ExcelRange.value属性是它包含了所有未格式化的对象的数组。但是,一旦你遍历在ExcelRange细胞,并要求cell.Text财产,它已被处理。试图修改ConditionalFormatting和DataValidation在ExcelRange没有帮助(FE AddContainsText()) - @EDIT - >不论是对于整个工作表: - (

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 :-(

我宁愿不投ExcelRange.Value为阵,它的丑陋,非常有条件的。

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

推荐答案

显然,这是解决方案(而不是完整的代码虽然,你要添加到数据表列)。我找不到格式字符串,它指定为Epplus无格式,但在这里,你有它。

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);
 }

这篇关于力EPPLUS阅读文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 12:01