1 读取Excel一定范围内的值时,指定起始行和终点行。
/// <summary>
/// 读取指定范围内的Excel,指定起始行和终止行
/// </summary>
/// <param name="filePath"></param>
/// <param name="sheetName"></param>
/// <param name="startColName"></param>
/// <param name="endColName"></param>
/// <param name="rowStartIndex"></param>
/// <param name="rowEndIndex"></param>
private static void ReadExcel(string filePath, string sheetName, uint rowStartIndex, uint rowEndIndex)
{
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(filePath, false))
{
WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
Workbook workbook = workbookPart.Workbook;
IEnumerable<Sheet> sheets = workbook.GetFirstChild<Sheets>().Elements<Sheet>().Where(s => s.Name == sheetName);
if (sheets.Count() == 0)
{
return;
}
string relationshipId = sheets.First().Id.Value;
WorksheetPart worksheetPart = (WorksheetPart)spreadsheetDocument.WorkbookPart.GetPartById(relationshipId);
Dictionary<string, string> dicResult = new Dictionary<string, string>();
IEnumerable<Row> rows = worksheetPart.Worksheet.GetFirstChild<SheetData>().Elements<Row>();
if (rows.Count() == 0)
{
return;
}
foreach (Row row in rows)
{
if (rowStartIndex<=row.RowIndex&& row.RowIndex<=rowEndIndex)
{
IEnumerable<Cell> cells = row.Elements<Cell>();
if (cells.Count() == 0)
{
continue;
}
else
{
foreach (Cell cell in cells)
{
dicResult.Add(cell.CellReference, cell.CellValue.Text);
}
}
}
}
foreach (var dic in dicResult)
{
Console.WriteLine($"单元格:{dic.Key}的值:{dic.Value}");
}
}
}
2 读取Excel一定范围内的值时,指定起始行和终点行,起始列的字符,终点列的字符。
/// <summary>
/// 读取指定范围内的Excel,指定起始行和终止行,指定起止列
/// </summary>
/// <param name="filePath"></param>
/// <param name="sheetName"></param>
/// <param name="startColName"></param>
/// <param name="endColName"></param>
/// <param name="rowStartIndex"></param>
/// <param name="rowEndIndex"></param>
private static void ReadExcel(string filePath, string sheetName, uint rowStartIndex, uint rowEndIndex,char cellStartIndex, char cellEndIndex)
{
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(filePath, false))
{
WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
Workbook workbook = workbookPart.Workbook;
IEnumerable<Sheet> sheets = workbook.GetFirstChild<Sheets>().Elements<Sheet>().Where(s => s.Name == sheetName);
if (sheets.Count() == 0)
{
return;
}
string relationshipId = sheets.First().Id.Value;
WorksheetPart worksheetPart = (WorksheetPart)spreadsheetDocument.WorkbookPart.GetPartById(relationshipId);
Dictionary<string, string> dicResult = new Dictionary<string, string>();
IEnumerable<Row> rows = worksheetPart.Worksheet.GetFirstChild<SheetData>().Elements<Row>();
if (rows.Count() == 0)
{
return;
}
foreach (Row row in rows)
{
if (rowStartIndex <= row.RowIndex && row.RowIndex <= rowEndIndex)
{
IEnumerable<Cell> cells = row.Elements<Cell>();
if (cells.Count() == 0)
{
continue;
}
else
{
foreach (Cell cell in cells)
{
if(cellStartIndex<= GetColumnName(cell.CellReference.Value).First()&& GetColumnName(cell.CellReference.Value).First()<=cellEndIndex)
{
dicResult.Add(cell.CellReference, cell.CellValue.Text);
}
}
}
}
}
foreach (var dic in dicResult)
{
Console.WriteLine($"单元格:{dic.Key}的值:{dic.Value}");
}
}
}
private static uint GetRowIndex(string cellName)
{
// Create a regular expression to match the row index portion the cell name.
Regex regex = new Regex(@"\d+");
Match match = regex.Match(cellName);
return uint.Parse(match.Value);
}
private static string GetColumnName(string cellName)
{
// Create a regular expression to match the row index portion the cell name.
Regex regex = new Regex(@"[A-Z]");
Match match = regex.Match(cellName);
return match.Value;
}