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;
}

[OpenXml] Read/Write row/cell from excel的更多相关文章

  1. OpenXML - 如何导出List<DataModel>到Excel -- Part 1

    最近这几天研究OpenXML: 这是Open XML的一些介绍: Open XML 介绍:http://baike.baidu.com/view/1201978.htm 下载:http://www.m ...

  2. Aspose.cell处理Excel

    (一)从数据库中读取数据写入Excel中 方法1: 步骤:1.建立一个新的项目,引用动态链接库Aspose.dll 2.见下面的原代码 using System;using System.Collec ...

  3. 使用aspose.cell导出excel需要注意什么?

    1.如果导出的数据源是汇总出来的,最好方法是将数据源放到缓存里面,当基本数据源变化的时候,在改变数据2.使用模板导出EXCEL,这样很多样式可以在模板文件里面直接设置,例如:默认打开页签,让列头固定3 ...

  4. aspose.cell 设置excel里面的文字是超链接

    目的: 1.通过方法designer.Workbook.Worksheets[0].Hyperlinks.Add("A1", 1, 1, url);给导出到excel里面的数据加上 ...

  5. aspose.cell制作excel常见写法

    //设置Excel的基本格式信息 Workbook workbook = new Workbook(); Worksheet worksheet = workbook.Worksheets[]; St ...

  6. Excel 用row()函数 在Excel中自动添加序号,

    1.如图 2.用if条件根据产品名称判断是否有值进而序号自动添加 If(G9="","",Row()-8)

  7. 使用Asponse.Cell解决Excel科学计数法问题

    //fileName --文件路径 public DataSet DataSetGetDataFromExcel(string fileName) { DataSet dataset = new Da ...

  8. aspose.cell 给excel表格设置样式

    方法1: Style styleTitle = workbook.Styles[workbook.Styles.Add()];//新增样式 styleTitle.HorizontalAlignment ...

  9. Find Blank Cell in Excel

    Click Home > Find & Select > Go To Special. In the Go To Special dialog box, check the Bla ...

随机推荐

  1. 机器学习中的范数规则化之(一)L0、L1与L2范数 非常好,必看

    机器学习中的范数规则化之(一)L0.L1与L2范数 zouxy09@qq.com http://blog.csdn.net/zouxy09 今天我们聊聊机器学习中出现的非常频繁的问题:过拟合与规则化. ...

  2. C#_自动测试2_连连看外挂

    GUI自动化测试和做外挂的原理很相似,都是模拟用户的鼠标和键盘操作, 给自己的程序写自动化就是做测试,给别人的程序写自动化就是外挂了. 本文使用的技术也同样适用制作“对对碰”,"找茬&quo ...

  3. 利用 Composer 完善自己的 PHP 框架(一)——视图装载

    本教程示例代码见 https://github.com/johnlui/My-First-Framework-based-on-Composer 回顾 经过了上一个 系列教程  <利用 Comp ...

  4. C语言第四节数据类型、常量、变量

    数据 什么是数据 生活中时时刻刻都在跟数据打交道,比如体重数据.血压数据.股价数据等.在我们使用计算机的过程中,会接触到各种各样的数据,有文档数据.图片数据.视频数据,还有聊QQ时产生的文字数据.用迅 ...

  5. Android(java)学习笔记104:Map集合的遍历之键找值

    package cn.itcast_01; import java.util.HashMap; import java.util.Map; import java.util.Set; /* * Map ...

  6. html/css 两个div在同一行

    在界面设计的时候,经常需要将两个div在同一行显示. 但是每次都会忘记怎么做,特此随笔,备忘. 如以下要将“第一个div”和“第二个div”显示在同一行: <div id="id1&q ...

  7. 完全自定义 TabBar

    // // CustomTabBarController.h // Dream // // Created by mac on 14-10-17. // Copyright (c) 2014年 HM. ...

  8. 安卓Design包之TabLayout控件的使用

    转自: 安卓Design包之TabLayout控件的简单使用 Google在2015的IO大会上,给我们带来了更加详细的Material Design设计规范,同时,也给我们带来了全新的Android ...

  9. jquery 实现图片跳动。提示作用

    需要做几件事: 第一:样式表定义 .red { border: 2px solid #ffffff; } 第二:定义抖动函数 function shake(ele, cls, times) { var ...

  10. python(1) -文件操作

    很多时候我们需要对文件进行一些操作,比如读取并分析日志文件,写入日志文件等等.显然python也内置了对文件进行操作的函数. 读文件: >>> f = open('a.log','r ...