Toan Nguyen Phuoc
Toan Nguyen Phuoc

Reputation: 306

How to read file excel with empty cells using EPPlus

I have a file excel with 2 columns: Name, price. I using EPPlus to read file excel :

OpenFileDialog dlg = new OpenFileDialog();
var package = new ExcelPackage(new FileInfo("" + dlg.FileName));
ExcelWorksheet workSheet = package.Workbook.Worksheets[1];
for (int i = workSheet.Dimension.Start.Row + 1; i <= workSheet.Dimension.End.Row; i++)
{
    try
    {
         int j = 1;
         string name = workSheet.Cells[i, j++].Value.ToString();
         string price = workSheet.Cells[i, j++].Value == null ? string.Empty : workSheet.Cells[i, j++].Value.ToString();

I want to read file excel with empty cell from column price, I find many solutions and I saw a solution like that:string price = workSheet.Cells[i,j++].Value == null ? string.Empty : workSheet.Cells[i, j++].Value.ToString();

I debugged and I saw price = true.

And I used OriPrice= decimal.Parse(price) (OriPrice is a name of column in table in database) to add value from excel to database. But I have an error.

I know error in line string price = workSheet.Cells[i,j++].Value == null ? string.Empty : workSheet.Cells[i, j++].Value.ToString(); because price = true, I want to price = string.Empty or workSheet.Cells[i, j++].Value.ToString();

But I don't how to do that.

Upvotes: 1

Views: 1375

Answers (1)

Richard   Housham
Richard Housham

Reputation: 1680

Try just

string price =  workSheet.Cells[i, j++].Text.Trim() 

No if else, should work. I think.

Upvotes: 1

Related Questions