zoukankan      html  css  js  c++  java
  • [OpenXml] Read/Write row/cell from 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;
            }
    
  • 相关阅读:
    magent + memcached 集群测试
    SQL Server 2000/2005检测存储过程名是否存在,存在删除
    ASP.NET在线用户列表精确版——解决用户意外退出在线列表无法及时更新问题
    使用asp.net/c# ajax 乱码的解决办法
    清空删除mssql数据库日志原文网址:http://admin.88443.net/article.
    net2.0下的简繁转换
    创建和注册自定义 httpModules 模块
    监控用户是否关闭浏览器
    Asp.net(Ajax)表单验证 函数包
    IE自带的网页过渡特效
  • 原文地址:https://www.cnblogs.com/webglcn/p/4679920.html
Copyright © 2011-2022 走看看