[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 ...
随机推荐
- Ruby on Rails Tutorial 第四章 Rails背后的Ruby 之 其他数据类型(二)
1.方法 定义如下所示: def string_message(str='') if str.empty? "It's an empty string!" else "T ...
- MYSQL 分析表、检查表和优化表
1. 对表进行优化 ( 优化表主要作用是消除删除或者更新造成的空间浪费) 2. 对表进行分析(分析关键字的分布, 分析并存储MyISAM和BDB表中键的分布) 3. 对表进行检查(检查表的错误,并且为 ...
- PHP.1-网站开发概述
网站开发概述 网站开发从本质来说,就是软件开发 1.B/S软件体系统结构 BS:浏览器与服务器的结构[降低客户端电脑的负荷,减轻维护成本,对CS的改进,可随时随地进行业务处理] #对美工要求比较高,注 ...
- 加载程序到android虚拟机报错: android.widget.RelativeLayout cannot be cast to android.widget.Button
05-23 02:53:48.416: E/Trace(875): error opening trace file: No such file or directory (2) 05-23 02:5 ...
- 无限滚动 --demo
<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content=&q ...
- asp.net session容易丢失解决方案
web Form 网页是基于HTTP的,它们没有状态, 这意味着它们不知道所有的请求是否来自 同一台客户端计算机,网页是受到了破坏,以及是否得到了刷新,这样就可能造成信息的 丢失. 于是, 状态管理就 ...
- oracle数据库性能调优
一:注意WHERE子句中的连接顺序: ORACLE采用自下而上的顺序解析WHERE子句,根据这个原理,表之间的连接必须写在其他WHERE条件之前, 那些可以过滤掉最大数量记录的条件必须写在WHERE子 ...
- (转)嵌入式学习准备---linux c 文件锁
(1)fcntl函数说明 前面的这5个基本函数实现了文件的打开.读写等基本操作,这一节将讨论的是,在文 件已经共享的情况下如何操作,也就是当多个用户共同使用.操作一个文件的情况,这时,Linux 通常 ...
- IE Firefox Safari 下 通过Div“隐藏”设置Accesskey的submit input
实现效果: 进入页面后 通过快捷键 如Alt+C 调用相关的隐藏按钮 实现功能操作 正常情况下 设置 button => input type="submit" acces ...
- [改善Java代码]不要在构造函数中抛出异常
Java的异常机制有三种: 一.Error类以及其子类表示的是错误,它是不需要程序员处理也不能处理的异常.比如VirtualMachineError虚拟机错误,ThreadDeath线程僵尸等. 二. ...