Tag: openxml sdk

使用OpenXML读取Excel文件

我正试图通过OpenXML读取Excel文件,并希望输出到CSV。 目前它读取每个单元格在一个单独的行(由于writeline)或单行(当使用写)。 以Excel等表格forms读取和输出的最佳方式是什么? OpenXML中是否有内置的function,我可以利用这个function? static void Main(string[] args) { String xlDocName = @"C:\Users\xlp111\source.xlsx"; using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(xlDocName, false)) { WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart; string cellValue = string.Empty; foreach(WorksheetPart worksheetPart in workbookPart.WorksheetParts) { OpenXmlReader reader = OpenXmlReader.Create(worksheetPart); while (reader.Read()) { if (reader.ElementType == typeof(Row)) { reader.ReadFirstChild(); do { if (reader.ElementType == typeof(Cell)) { Cell c […]

使用现有的范围名称从电子表格读取或检索值

我有一个现有的Excel文件,它已经定义了单元格名称或单元格区域名称。 我能够使用Openxml sdk获取所有单元格区域名称。 我的示例代码如下: var path = @"D:\test.xlsx"; using (var document = SpreadsheetDocument.Open(path, true)) { var workbookPart = document.WorkbookPart; var wb = workbookPart.Workbook; var definedNames = wb.DefinedNames; if (definedNames != null) { System.Console.WriteLine("Name\tText\tName.Value"); foreach (DefinedName dn in definedNames) { System.Console.WriteLine(dn.Name + "\t" + dn.Text + "\t" + dn.Name.Value); } } } 有没有什么办法来检索或使用这个OpenXml或任何其他SDK在C#中定义的名称读取单元格的值?

1U在C#和OPEN XML SDK 2.0中的含义

我总是看到C#中的1U代码,特别是在OPEN XML SDK 2.0中的风格。 有人知道它的意思吗?

使用SharedStringTable在.net中使用xml sdk设置文本值

我有一段代码(下面)可以在Excel中获取特定单元格的文本,但是我不知道如何修改此文本来更改单元格文本。 public static void UpdateTextCell(string docName, string text, uint rowIndex, string columnName, string sheetName) { // Open the document for editing. using (SpreadsheetDocument spreadSheet = SpreadsheetDocument .Open(docName, true)) { WorksheetPart worksheetPart = GetWorksheetPartByName(spreadSheet, sheetName); if (worksheetPart != null) { SharedStringTablePart sharedstringtablepart=spreadSheet.WorkbookPart .GetPartsOfType<SharedStringTablePart>().First(); SharedStringTable sharedStringTable = sharedstringtablepart .SharedStringTable; Cell cell = GetCell(worksheetPart.Worksheet, columnName, rowIndex); if (cell.DataType.Value […]

我怎样才能十进制值的精度达到一个精度,使数字的总数不跨越15?

我需要将25979.699999999997这样的值整理成15个数字,所以它变成了25979.7,并且不包含任何微不足道的零。 背景:我试图parsing一个使用OpenXML SDK的Excel文件,并且有一个十进制值,如25979.7读为25979.699999999997。 当MS excel显示值时,它总是舍入到15位数,并删除尾随的零,所以它变成25979.7,但是当我使用OpenXML SDK读取时,它不会自动执行。 任何想法? 编辑:其实我不想修改的价值,只是想显示在该格式(作为一个string)。

Excel单元格types是“数字”,但仍显示为“常规”

我正在使用C#中的OpenXML电子表格包将datatable转换为excel。 在代码中我写这个附加单元格与数字types: public void MakeNumericCell(string cellReference, string cellStringValue, Row excelRow) { // Add a new Excel Cell to our Row Cell cell = new Cell() { CellReference = cellReference, DataType=CellValues.Number }; CellValue cellValue = new CellValue(cellStringValue); cell.Append(cellValue); excelRow.Append(cell); } 请注意,我不是问如何将文本单元格转换为数字或更改数据types(格式)的Excel单元格,其答案已经存在于SO上。 在我的情况下,单元格转换为Excel表格中的数字格式。 我可以通过使用formula =Type(Cell)findExcel单元格的types,如果单元格types是数字,则返回1。 如图所示,最后一列(列的名称是Type of Cell )是使用=Type(cell)公式对C列返回1来计算的,但是它仍然以图像中突出显示的Generalforms出现。 我在这里做错了什么? 或者这是Excel本身的问题?

使用openxml sdk将图像添加到excel中

我试图添加一个图像到excel文档,该文档是在生成电子表格之后使用openxml生成的(因为closedxml当前无法插入图像)。 我可以正确生成excel文件,但打开罚款,但是当我尝试插入图像,并尝试在Excel中打开它会给出一个错误。 我使用了一个小的控制台应用程序,发现错误是 Error 1 Description: The element has unexpected child element 'http://schemas.openxmlfor mats.org/spreadsheetml/2006/main:drawing'. Path: /x:worksheet[1] Part: /xl/worksheets/sheet.xml ——————————————- 电子表格如下所示: <?xml version="1.0" encoding="utf-8"?> <x:worksheet xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"> <x:sheetPr> <x:outlinePr summaryBelow="1" summaryRight="1" /> </x:sheetPr> <x:dimension ref="A1:M4" /> <x:sheetViews> <x:sheetView tabSelected="0" workbookViewId="0" /> </x:sheetViews> <x:sheetFormatPr defaultRowHeight="15" /> <x:cols> … </x:cols> <x:sheetData> … </x:sheetData> <x:printOptions horizontalCentered="0" verticalCentered="0" headings="0" gridLines="0" […]

使用OpenXML SDK 2.0从Excel单元格中读取数据

我想通过这种方式从Excel单元格中获取值: SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(filePath, true); WorksheetPart worksheetPart = getWorksheetByName(spreadSheetDocument, DEFAULT_SHEET_NAME); SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>(); Cell theCell1 = worksheetPart.Worksheet.Descendants<Cell>().FirstOrDefault(c => c.CellReference == "A5"); Cell theCell2 = worksheetPart.Worksheet.Descendants<Cell>().FirstOrDefault(c => c.CellReference == "A6"); Cell theCell3 = worksheetPart.Worksheet.Descendants<Cell>().FirstOrDefault(c => c.CellReference == "B5"); Cell theCell4 = worksheetPart.Worksheet.Descendants<Cell>().FirstOrDefault(c => c.CellReference == "B6"); 然后我检查了CELL1.CellValue.Text propetry,我得到了一些奇怪的数据,例如4,5,248等,这实际上远不是真实的数据。 我可以使用Excel查看和编辑的实际值。 有没有人猜测为什么呢?

用Epplus获得excel单元格的样式

有没有什么办法来打开特定的单元格与开放的XML SDK和epplus? 目前我正在以如下方式获得价值: currentWorksheet.Cells[nRowId, 3].Text.Trim(); 为了这个,我想如果有背景颜色和边框。

使用OpenXML在Excel单元格中添加date

这就是我所做的: CellFormat cellFormat = new CellFormat() { NumberFormatId = (UInt32Value)14U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U, ApplyNumberFormat = true }; sd.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.AppendChild<CellFormat>(cellFormat); _dateStyleIndex = sd.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.Count() – 1; 然后在我的代码稍后的某个地方 else if (type == DataTypes.DateTime) { DateTime dateTime = DateTime.Parse(text); double oaValue = dateTime.ToOADate(); cell.CellValue = new CellValue(oaValue.ToString(CultureInfo.InvariantCulture)); cell.DataType = new EnumValue<CellValues>(CellValues.Date); […]