[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;
}
[OpenXml] Read/Write row/cell from excel的更多相关文章
- OpenXML - 如何导出List<DataModel>到Excel -- Part 1
最近这几天研究OpenXML: 这是Open XML的一些介绍: Open XML 介绍:http://baike.baidu.com/view/1201978.htm 下载:http://www.m ...
- Aspose.cell处理Excel
(一)从数据库中读取数据写入Excel中 方法1: 步骤:1.建立一个新的项目,引用动态链接库Aspose.dll 2.见下面的原代码 using System;using System.Collec ...
- 使用aspose.cell导出excel需要注意什么?
1.如果导出的数据源是汇总出来的,最好方法是将数据源放到缓存里面,当基本数据源变化的时候,在改变数据2.使用模板导出EXCEL,这样很多样式可以在模板文件里面直接设置,例如:默认打开页签,让列头固定3 ...
- aspose.cell 设置excel里面的文字是超链接
目的: 1.通过方法designer.Workbook.Worksheets[0].Hyperlinks.Add("A1", 1, 1, url);给导出到excel里面的数据加上 ...
- aspose.cell制作excel常见写法
//设置Excel的基本格式信息 Workbook workbook = new Workbook(); Worksheet worksheet = workbook.Worksheets[]; St ...
- Excel 用row()函数 在Excel中自动添加序号,
1.如图 2.用if条件根据产品名称判断是否有值进而序号自动添加 If(G9="","",Row()-8)
- 使用Asponse.Cell解决Excel科学计数法问题
//fileName --文件路径 public DataSet DataSetGetDataFromExcel(string fileName) { DataSet dataset = new Da ...
- aspose.cell 给excel表格设置样式
方法1: Style styleTitle = workbook.Styles[workbook.Styles.Add()];//新增样式 styleTitle.HorizontalAlignment ...
- Find Blank Cell in Excel
Click Home > Find & Select > Go To Special. In the Go To Special dialog box, check the Bla ...
随机推荐
- 图形化管理debian服务
bootupmanager这个软件 ,用着勉强吧, 功能不多. 安装 sudo apt-get install bum 卸载sudo apt-get remove --purge bum 多了不说 , ...
- JDK1.6官方下载_JDK6官方下载_JDK1.6API(chm)下载_JDK6APICHM中文参
JDK1.6官方下载_JDK6官方下载地址:http://www.java.net/download/jdk6/6u10/promoted/b32/binaries/jdk-6u10-rc2-bin- ...
- eclipse ctrl+左击不能关联相应文件
<?xml version="1.0" encoding="UTF-8"?><projectDescription> <name& ...
- c语言海量数据处理
教你如何迅速秒杀掉:99%的海量数据处理面试题 http://wenku.baidu.com/view/4546d06ca45177232f60a276.html c语言如何对海量数据进行处理 PDF ...
- ESX虚拟机文件列表详解
http://jackiechen.blog.51cto.com/196075/210492 关闭状态时的文件列表: *-flat.vmdk:虚拟机的原始磁盘文件,包含整个虚拟机镜像. *.nvr ...
- tornado简单的验证码
1.html代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- MAC下查看端口占用并杀死进程
Eclipse在Run on Server时,Tomcat是开启的,但是报错,显示8080.8005和8009端口被占用 终端输入 查看所有开启的端口 sudo lsof -i -P | grep - ...
- 深入理解计算机系统第二版习题解答CSAPP 2.20
T2Uw(w)=x, x≥0时 T2Uw(w)=x+2w, x<0时 利用上面的公式,重新计算2.19的问题.
- [BigData]关于Hadoop学习笔记第四天(PPT总结)(一)
课程安排 Partitioner编程** 自定义排序编程** Combiner编程** 常见的MapReduce算法** ---------------------------加深拓展-------- ...
- applicationContext-mail.xml 模板
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...