1

I am using nuget Package named ExcelDataReader in MVC application to read Excel sheet and convert it to HTML.All sounds goof but i am not able to read special character from excel sheet.For example is cell value is "59%" but its reading 0.59.So how can i get exact same string value from excel.I am using following code in Controller to read and display excel to html.

public ActionResult ViewExcelFileData(long? id, int sheetindex)    
 {
 var excelfile = db.ExcelUpload.FirstOrDefault(x => x.FileID == id);
string filePath=        string.Format(Server.MapPath("~/App_Data/ExcelUploads/Labor_Excel/") + excelfile.FileName);
System.IO.File.GetAccessControl(filePath);
FileStream stream = System.IO.File.Open(filePath, FileMode.Open, FileAccess.Read);
IExcelDataReader reader = null;
if (excelfile.FileName.EndsWith(".xls"))
{
 reader = ExcelReaderFactory.CreateBinaryReader(stream);
}
else if (excelfile.FileName.EndsWith(".xlsx"))
{
 reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
}
reader.IsFirstRowAsColumnNames = true;

DataSet result = reader.AsDataSet();
reader.Close();
return View(result.Tables[sheetindex]);
 }

By returning datatable in View side I am using Following Code in Cshtml.

<table class="gridtable" id="table-1">
                <thead class="fixed_headers">
                    <tr>
                        @foreach (DataColumn col in Model.Columns)
                        {
                            <th>@col.ColumnName</th>
                        }
                    </tr>
                </thead>
                <tbody>
                    @foreach (DataRow row in Model.Rows)
                    {
                        <tr>
                            @foreach (DataColumn col in Model.Columns)
                            {                                    
                                    <td>@row[col.ColumnName]</td>                                    
                            }
                        </tr>
                    }
                </tbody>
            </table>

So is it possible in controller side or View side I can get 59% instead of 0.59?

Here is Two images in which one is belongs to excel and second is HTML.

IMAGE :1

Excel Image

IMAGE 2 :

HTML

Csharp
  • 177
  • 4
  • 15

2 Answers2

1

ExcelDataReader currently does not expose any formatting information, it provides you with the raw values except for dates that are converted to DateTime.

There's an open issue about extending it to provide formatting information, https://github.com/ExcelDataReader/ExcelDataReader/issues/215.

Johan Appelgren
  • 331
  • 1
  • 5
0

Instead of using the AsDataSet() method, you could read the row one by one and read the value as string. Drawback for this is you need to specify the columns one by one which is a maintainability issue.

while (reader.Read())
{
    var value = reader.GetString(0); //Get value of first column
}

I suspect that the AsDataSet() converts the data to its respective data type that's why you are seeing the converted decimal value instead of the actual value in the excel.

Bon Macalindong
  • 1,310
  • 13
  • 20