[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 ...
随机推荐
- Build类
在开发中 我们有时候会需要获取当前手机的系统版本来进行判断,或者需要获取一些当前手机的硬件信息. android.os.Build类中.包括了这样的一些信息.我们可以直接调用 而不需要添加任何的权限和 ...
- cocos2dx 背景用小尺寸图片滚动填充的方法
直接上代码 在初始化方法中添加图片: bool BackGroundLayer::init() { frameCache=CCSpriteFrameCache::sharedSpriteFrameCa ...
- 嵌入式 Linux 应用:概述
转载:http://www.ibm.com/developerworks/cn/linux/embed/embl/overview/index.html 从腕表到基于群集的超级计算机 在对嵌入式 ...
- Advice on improving your programming skills
Programming is cool. But behind the scenes it's also difficult for many people. Many people are defe ...
- Debian7.7 wheezy 中源码安装emacs24
我用的是ARM版本,竟然没有最新的emacs 24,很多第三方插件不能用,果断重新编译个1.追加软件源 deb-src http://ftp.cn.debian.org/debian/ wheezy ...
- hadoop学习记录(三)HBase基本概念
这一次开始学习HBase数据库. 我用的是VMWare + ubuntu16.04 +Hbase1.1.5 +hadoop2.6.0的组合. 经过亲自安装验证,版本间没有发生冲突,可以进行学习和开发. ...
- Ajax返回html和json格式数据
Ajax可以返回text和xml格式 可以用Ajax返回大段的html文本和json格式的字符串,然后用eval()方法 转化为json对象 php中的json编码:json_encode(); ph ...
- InternetOpen怎么使用代理
如果你用IE的默认代理设置:hinternet=InternetOpen(AfxGetAppName(),INTERNET_OPEN_TYPE_PROXY,NULL,NULL,0); 把INTERNE ...
- 【Cocos2d入门教程一】Cocos2d-x环境搭建
在进行Cocos2d游戏开发前 我们先来配置一下环境,我们先来准备一下工具,我们所需要的工具分别为: 1.Cocos2d引擎 2.JDK 3.SDK 4.NDK 5.ANT 6.ADT 1.下载Coc ...
- python学习day4--python基础--字典
字典的常用操作: #字典天然去重,key唯一,如果key相同,只能打印出一个 id_db={ 220456789852963741:{ 'name':"alex", 'age':3 ...