[OpenXml] Read/Write row/cell from excel
                                            
 3496 단어  Excel
                    
        public static void test(){
            using (SpreadsheetDocument document = SpreadsheetDocument.Open("test.xlsx", true))
            {
                WorkbookPart workbookPart = document.WorkbookPart;
                string relId = workbookPart.Workbook.Descendants<Sheet>().First(sheet => sheet.Name.Value.ToLower().Equals("sheet1")).Id;
                WorksheetPart worksheetpart = (WorksheetPart)workbookPart.GetPartById(relId);
                DocumentFormat.OpenXml.Spreadsheet.Worksheet worksheet = worksheetpart.Worksheet;
                SheetData sheetData = worksheet.GetFirstChild<SheetData>();
                for (int i = 2; i <= 3; i++)
                {
                    Row row = new Row() { RowIndex = (uint)i };
                    row.Append(new Cell() { CellReference = "A" + i, DataType = CellValues.String, CellValue = new CellValue("kaka") });//, StyleIndex = styleIndex });
                    row.Append(new Cell() { CellReference = "B" + i, DataType = CellValues.String, CellValue = new CellValue("2") });//, StyleIndex = styleIndex });
                    row.Append(new Cell() { CellReference = "C" + i, DataType = CellValues.String, CellValue = new CellValue("GENERAL MANAGEMENT") });//, StyleIndex = styleIndex });
                    row.Append(new Cell() { CellReference = "D" + i, DataType = CellValues.String, CellValue = new CellValue("2") });//, StyleIndex = styleIndex });
                    row.Append(new Cell() { CellReference = "E" + i, DataType = CellValues.String, CellValue = new CellValue((1 == 1).ToString()) });//, StyleIndex = styleIndex });
                    sheetData.Append(row);
                }
                // loop each row to get value;
                string cellValue = GetCellValue(sheetData.Descendants<Row>().ElementAt<Row>(0), "A", getSharedString(document));
                worksheet.Save();
            }
        }
        private static List<SharedStringItem> getSharedString(SpreadsheetDocument document)
        {
            WorkbookPart workbookPart = document.WorkbookPart;
            return workbookPart.SharedStringTablePart.SharedStringTable.Elements<SharedStringItem>().ToList<SharedStringItem>();
        }
        // cell could be null
        private static Cell GetCell(Row row, string columnName)
        {
            return row.Descendants<Cell>().FirstOrDefault(p => p.CellReference == columnName + row.RowIndex);
        }
        // call getSharedString
        // call GetCell
        // => retrieve cell value
        public static string GetCellValue(Row row, string columnName, List<SharedStringItem> sharedStrings)
        {
            Cell cell = GetCell(row, columnName);
            if (cell == null)
            {
                return null;
            }
            string value = "";
            if (cell.DataType != null && cell.DataType.Value == CellValues.SharedString)
            {
                SharedStringItem item = sharedStrings.ElementAt<SharedStringItem>(Int32.Parse(cell.CellValue.Text));
                value = item.InnerText;
            }
            else
            {
                value = cell.CellValue.Text;
            }
            return value;
        }
  이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Excel Grep toolExcel Grep tool ■히나가타 ■ 시트 구성 ExcelGrep.cls...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.