Reputation: 306
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
Reputation: 1680
Try just
string price = workSheet.Cells[i, j++].Text.Trim()
No if else, should work. I think.
Upvotes: 1