/// <summary> /// 读取、解析 /// </summary> /// <param name="fileName">文件</param> /// <param name="sheetName">工作表(默认第一个)</param> /// <param name="type">1 不去空格 2 前后空格 3 所有空格 </param> /// <returns></returns> public static List <ExcelCellInfo> Read(string fileName, string sheetName = "", int type = 2) { List <ExcelCellInfo> excelCellInfos = new List <ExcelCellInfo>(); using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(fileName, false)) { WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart; //查找工作薄 Sheet sheet = ExcelSeek.SeekSheet(workbookPart, sheetName); //工作表 WorksheetPart worksheetPart = (WorksheetPart)spreadsheetDocument.WorkbookPart.GetPartById(sheet.Id); //数据行 var rows = worksheetPart.Worksheet.Descendants <Row>();//获得Excel中得数据行 foreach (Row r in rows) { foreach (Cell c in r.Elements <Cell>()) { ExcelCellInfo excelCellInfo = new ExcelCellInfo(); excelCellInfo.RowIndex = (int)r.RowIndex.Value; excelCellInfo.CellReference = c.CellReference; excelCellInfo.ColumnIndex = ExcelAlphabet.ABCToColumn(excelCellInfo.CellReference.Replace(excelCellInfo.RowIndex.ToString(), "")); excelCellInfo.Value = GetCellValue(c, workbookPart, type); excelCellInfos.Add(excelCellInfo); } } } return(excelCellInfos); }
// Create a spreadsheet cell. private void CreateSpreadsheetCell(Worksheet worksheet, string cellName) { string columnName = GetColumnName(cellName); string strSplit1 = Regex.Replace(cellName, "[0-9]", "", RegexOptions.IgnoreCase); int rowIndex = ExcelAlphabet.ABCToColumn(strSplit1); IEnumerable <Row> rows = worksheet.Descendants <Row>().Where(r => r .RowIndex.Value == rowIndex); Row row = rows.First(); IEnumerable <Cell> cells = row.Elements <Cell>().Where(c => c.CellReference .Value == cellName); }
private string GetCellName(uint rowIndex, int columnIndex) { return(ExcelAlphabet.ColumnToABC(columnIndex) + rowIndex); }