- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经预先定义了 excel 格式,我需要将数据传递给 excel。我能够获得特定的工作表。但不知道如何将数据传递给单元格。
var excelDocument = new ExcelDocument();
var fileName = Guid.NewGuid();
string filePath = HttpContext.Current.Server.MapPath("~/Uploads/TemplateFiles/test.xlsx");
using (SpreadsheetDocument document =
SpreadsheetDocument.Open(filePath, false))
{
WorkbookPart workbookPart = document.WorkbookPart;
Workbook workbook = document.WorkbookPart.Workbook;
string sheetName = workbookPart.Workbook.Descendants<Sheet>().ElementAt(1).Name;
IEnumerable<Sheet> sheets = document.WorkbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == "Census Template for Import");
if (sheets.Count() == 0)
{
// The specified worksheet does not exist.
return null;
}
WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(sheets.First().Id);
SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
var excelRows = sheetData.Descendants<DocumentFormat.OpenXml.Spreadsheet.Row>().ToList();
int rowindex = 10;
foreach (var item in census)
{
//how to write the data in cell
rowindex++;
}
worksheetPart.Worksheet.Save();
workbookPart.Workbook.Save();
document.Close();
//worksheetPart.Worksheet.Save();
}
return filePath;
最佳答案
这是获取单元格或添加新单元格的方法,如果单元格不存在,当您知道行索引和列索引时。
注意:
Row
的属性 RowIndex
应在创建行期间初始化Cell
的属性 CellReference
应该在创建单元格时初始化如果RowIndex
或CellReference
为null,则抛出NullReferenceException。
private Cell InsertCell(uint rowIndex, uint columnIndex, Worksheet worksheet)
{
Row row = null;
var sheetData = worksheet.GetFirstChild<SheetData>();
// Check if the worksheet contains a row with the specified row index.
row = sheetData.Elements<Row>().FirstOrDefault(r => r.RowIndex == rowIndex);
if (row == null)
{
row = new Row() { RowIndex = rowIndex };
sheetData.Append(row);
}
// Convert column index to column name for cell reference.
var columnName = GetExcelColumnName(columnIndex);
var cellReference = columnName + rowIndex; // e.g. A1
// Check if the row contains a cell with the specified column name.
var cell = row.Elements<Cell>()
.FirstOrDefault(c => c.CellReference.Value == cellReference);
if (cell == null)
{
cell = new Cell() { CellReference = cellReference };
if (row.ChildElements.Count < columnIndex)
row.AppendChild(cell);
else
row.InsertAt(cell, (int)columnIndex);
}
return cell;
}
Here您将找到 GetExcelColumnName()
方法的代码。
关于c# - 在 openXML 中添加单元格和行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32308023/
我的 C# 应用程序使用 OpenXML 创建一个 MSWord 文档,其中已经包含多个表。最后一部分是添加条形图。我找不到这个案例的好例子。 谢谢你的帮助! 我正在从头开始创建文档。从...开始:
我正在使用一个模板文档,该文档使用 CustomXmlBlocks 作为占位符来放置表格和其他信息。我需要能够以某种方式将图像放入其中一个 block 中......即使它首先放入运行中。 图像以字节
我正在以编程方式将 OpenXML 段落添加到 Word 文档中,并且稍后我需要能够将该段落识别为我的段落。关于如何做到这一点有什么想法吗?我尝试插入 XML 注释和扩展属性,但是当您将文档保存在 w
我想使用 openxml 删除一列,我能够清除单元格的内容,但一直无法找到删除列的文档,以便在删除列时向左移动其他单元格。如何使用 openxml 删除列,它将单元格向左移动? 最佳答案 我发现 Op
我找不到任何可以告诉我 txBody 标签中的文本是否带有项目符号的指标,您能否请我确定我应该使用哪个指标来了解文本是项目符号还是普通文本? -谢谢 最佳答案 在 txBody 标签内,您可以查找标签
我目前正在尝试使用 PHPWord 库及其模板系统来处理 docx 文件。我已经找到并更新了这个库的某人(不记得名字,但它并不重要)的路径,该库可以使用表(复制其行,然后在每一行上使用 PHPWord
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: Open Xml and Date format in Excel cell 我正在尝试从 DataGridView
我发现各种元素非常困惑。几乎每个元素似乎都有一个与之关联的“部分”,我不确定它们是如何粘合在一起的。 工作簿工作簿部分工作表工作表部分 我也对 DocumentFormat.OpenXml.Packa
我有一个存储在 Bitmap 对象中的图像,我想将其粘贴到 OpenXML 文档中。我尝试使用 MemoryStream 作为中间步骤,如下所示: ImagePart part = container
我试图理解 OpenXML 电子表格的内部文件内容。在一些文件中,我发现了这个字符串。其他标签具有相同的前缀。标签也可以有前缀 p: w: 等。 你能帮我理解这些前缀在标签中的含义吗? 最佳答案 您可
我的预期结果是: 你好 世界! 但是当我使用以下代码时: MainDocumentPart mainDocumentPart = package.AddMainDo
我需要将多个 .docx 文件收集到一个文件夹中,并将它们“链接”成一个将显示给用户的文档。 现在我已经阅读了 Brian Jones' article虽然听起来很有希望,但我遇到了一个问题。 当我使
我正在使用 OpenXml 生成 Excel 文件,在研究了大量不同的示例代码和 SDK Productivity Tool 后,终于得到了我想要的东西。只有一件事我无法回避。当我使用 Excel 打
我正在使用 openxml 创建 WordProcessingDocuments(工作正常,生成的 word 文档正是我想要的),现在我正在尝试使用 openxml Powertools 将这些新创建
我使用 OpenXML SDK 2.5 编写了一个 Word 文档,当我在 MS Office 中预览该文档时,该文档给出了预期的外观和格式。 现在我需要将此文档转换为 HTML 文档,我开始了解 O
我是 .net 编码员,我对 ColdFusion 真的很陌生。我编写了一个自动生成发票的 .dll 库。我需要使用 ColdFusion 应用程序中的库。我已经成功地将我的库中的类加载为 coldf
以前我发布了一个问题,如何将数据集中的数据填充到 excel 工作表中,而不是如何使用该数据创建图表。那篇文章不太走运,但现在我设法从数据集中填充数据,但也在努力根据该数据创建图表,我希望图表与我的数
快要被这个问题搞疯了。我确信它是如此简单,我只是错过了它,但我一生都无法找出如何使用 C# 中的 OpenXml SDK v2.0 更改 Word 2007 中的内容控件的内容。 我创建了一个带有纯文
我正在编写一个小应用程序,它将路径作为输入,然后获取该路径中的每个 docx 文件,用关键字替换每个超链接。 奇怪的是,我发现了两种超链接,第一种来自 WordprocessingDocument E
我有一段文字想在文档的中央出现。如何在docx4j中执行此操作?我目前正在使用: PPr paragraphProperties = factory.createPPr(); //cr
我是一名优秀的程序员,十分优秀!