我已经找到了如何从具有特定工作表名称的Excel文件中读取第一行,但在我的情况下,我必须阅读第一张工作表的第一行。
我怎样才能意识到我必须使用CSV而不是XLS?
答案 0 :(得分:2)
你看过EPPlus library了吗?使用excel文件非常容易。
以下是使用EPPlus的方法:
FileInfo existingFile = new FileInfo("yourfilepathname");
using (var package = new ExcelPackage(existingFile))
{
ExcelWorkbook workbook = package.Workbook;
if (workbook != null && workbook.Worksheets.Count > 0)
{
//Gets the first worksheet. You can use index also to select your worksheet.
ExcelWorksheet worksheet = workbook.Worksheets.First();
int rowIndex = 0;
int colIndex = 0;
//To get the first cell:
var cellValue = worksheet.Cells[rowIndex, colIndex].Value;
//YOU CAN ALSO DO A LOOP TO GET ALL VALUES FROM THE FIRST ROW.
}
}
NPOI是另一个不错的选择,它也可以处理XLS文件。
以下是NPOI代码的片段:
HSSFWorkbook _hssfworkbook = new HSSFWorkbook(OpenFileStream(filePath));
//Getting the sheet
_currentSheet = _hssfworkbook.GetSheetAt(0);
//Getting the row:
_row = CurrentSheet.GetRow(0);
//Getting the cell
_cell = _row.GetCell(0);
答案 1 :(得分:0)
您考虑过SpreadsheetLight了吗?这是你如何做到的:
SLDocument sl = new SLDocument("YourExcelFile.xlsx", "TheSheetName");
string FirstLine = sl.GetCellValueAsString("A1");
sl.CloseWithoutSaving();
免责声明:我写了SpreadsheetLight。