I have to read Excel(.xlsx) file. I am using OpenXML NuGet pkg. I am trying to get the range( xlRange
), so I can fetch the data from a particular cell. But not getting success. This code is created in console environment.
static void Main(string[] args)
{
bool flagDataError = false;
string sDuplicateRows = string.Empty;
string sCreatedRows = string.Empty;
string sFileTypeError = string.Empty;
string filepath = @"C:\test.xlsx";
var fileName = Path.GetFileName(filepath);
var fileExtension = Path.GetExtension(filepath);
if ((fileExtension != ".xlsx") && (fileExtension != ".xls"))
sFileTypeError = "Invalid file. \n\nPlease browse a correct Excel file to upload.";
else
{
using (SpreadsheetDocument doc = SpreadsheetDocument.Open(filepath, false))
{
WorkbookPart workbookPart = doc.WorkbookPart;
WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
List<AxisData> data = new List<AxisData>();
for (int i = 5; i <= 16; i++)
{
for (int j = 2; j <= 13; j++)
{
AxisData axisDetails = new AxisData();
axisDetails.ProductCode = "AXIS " + xlRange.Cells[i, 1].Value;
axisDetails.ContractPeriod = Convert.ToDateTime(xlRange.Cells[4, j].Value);
axisDetails.SettlePrice = Convert.ToDecimal(xlRange.Cells[i, j].Value);
axisDetails.EffectiveStartDate = efectiveStartDate;
}
}
}
}
}
}
}
IEnumerable<Row> ShRows = sheetData.Elements<Row>();
var _RCount = SHRows.Count();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.