我在从Excel 2010 工作表读取值时遇到一些问题。
\n\n在标准 Excel 2010 工作表上,我有一个单元格,其货币格式带有两位小数和 value 1270,14 \xe2\x82\xac。
当我在 OpenXML 2.0(C# 代码)上读取此值时,我得到的1270.1400000000001不是原始的1270.14.
对于具有相同格式的任何单元格上的其他值,也会发生同样的情况。
\n\n从单元格 OpenXML 代码中获取值:
\n\nprivate string GetCellValue(string column, int row)\n{\n column = column.ToUpper();\n var targetCell = cells.Where(p => p.CellReference == (column + row)).SingleOrDefault();\n\n var value = String.Empty;\n\n if (targetCell.DataType != null && targetCell.DataType.Value == CellValues.SharedString)\n {\n var index = int.Parse(targetCell.CellValue.Text);\n value = cellValues[index].InnerText.Trim();\n }\n else\n {\n if (targetCell.CellValue != null)\n {\n value = targetCell.CellValue.Text.Trim();\n }\n else\n …Run Code Online (Sandbox Code Playgroud) 我试图让 xlsx 文件的前 4 行在打印时在每页顶部重复。我正在使用 Open XML SDK 来完成此任务。
我的文件由 SSRS 执行 Web 服务作为文件流生成。然后,在将文件交付给用户之前,我需要修改多个布局和页面设置(页边距、页面大小、缩放等),因为我的用户对 Excel 的了解很少。我需要调整的所有其他设置都可以正常工作,因此我已从此示例中删除了所有代码。
当尝试使用下面的代码设置重复页眉时,它执行时没有错误。但是,当我在执行代码后尝试打开文件时,我收到一条文件损坏消息,并且无法打开。
知道我在这里做错了什么吗?这让我疯狂!
using (SpreadsheetDocument xl = SpreadsheetDocument.Open("C:\\" + filename, true))
{
WorkbookPart wbp = xl.WorkbookPart;
//-----------------------------------------
// repeat rows at top when printing
//-----------------------------------------
DefinedNames dn = new DefinedNames();
wbp.Workbook.Append(dn);
wbp.Workbook.Save();
DefinedNames definedNames = wbp.Workbook.Descendants<DefinedNames>().FirstOrDefault();
DefinedName n1 = new DefinedName() { Name = "_xlnm.Print_Titles", LocalSheetId = (UInt32Value)0U };
n1.Text = "alpha_sort_nc!$1:$4";
definedNames.Append(n1);
wbp.Workbook.Save();
xl.Close();
}
Run Code Online (Sandbox Code Playgroud) 我正在编写 c# 程序来使用 OpenXML 读取 excel 文件。我在 Excel 文件的一列中添加了一个单词列表。我想读取它们并添加到数组列表中。我正在使用下面的代码
using (SpreadsheetDocument doc = SpreadsheetDocument.Open(filePatah + "\\" + fileName, false))
{
WorkbookPart workbookPart = doc.WorkbookPart;
WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
ArrayList data = new ArrayList();
foreach (Row r in sheetData.Elements<Row>())
{
foreach (Cell c in r.Elements<Cell>())
{
data.Add(c.CellValue.Text);
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我期待列出这些单词时,我看到的是 1,2,3,4 等。我需要做什么才能得到这些词?
所以我有一个正在通过 WPF(C#) 应用程序编辑的文档。我已成功编辑纯文本内容控件,但现在我无法选中/取消选中表单中的复选框。
我成功找到了复选框并设置了值并保存了文档,但是当我打开它时,从未在 word 文档中选中设置为 true 的复选框。
这是我用来操作复选框的代码。注意:我在标签级别访问复选框,因此 field.parent.parent
private static void SetCheckBox(OpenXmlElement field, bool isChecked)
{
var checkBox = field.Parent.Parent.Descendants<SdtContentCheckBox>().ToList();
foreach (var check in checkBox)
{
if (isChecked)
{
check.Checked.Val = OnOffValues.True;
}
else
{
check.Checked.Val = OnOffValues.False;
}
MessageBox.Show(check.Checked.Val);
}
}
Run Code Online (Sandbox Code Playgroud)
当我在 MessageBox 中显示值时,它们显示 0/1 表示真/假。所以它们实际上是被设置的。
我这样做正确吗?
我已经预先定义了 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 …Run Code Online (Sandbox Code Playgroud) 我想在打开的 Excel 文档中设置文本的前景色以写入文本。
为此我尝试过:
var stylesheet1 = spreadSheet.WorkbookPart.WorkbookStylesPart.Stylesheet;
Fills fills1 = new Fills() { Count = (UInt32Value)5U };
Fill fill5 = new Fill();
PatternFill patternFill5 = new PatternFill() { PatternType = PatternValues.Solid };
ForegroundColor foregroundColor3 = new ForegroundColor() { Rgb = "#FF0000" };
patternFill5.Append(foregroundColor3);
fill5.Append(patternFill5);
fills1.Append(fill5);
stylesheet1.Fills.Append(fills1);
var fid = stylesheet1.Fills.Count++;
wbsp.Stylesheet = stylesheet1;
Row excelRow;
excelRow = new Row();
excelRow.RowIndex = 0;
Cell cell = new Cell()
{
//create the cell reference of format A1, B2 etc
//CellReference = …Run Code Online (Sandbox Code Playgroud) 如何创建启用跟踪更改的 .docx?我被告知word/settings.xml我应该修改w:proofState但我发现的 OOXML 在线文档中的所有信息都与语法和拼写检查有关,但与启用跟踪更改无关。
我正在尝试使用 OpenXML 和 Eric White 的 OpenXmlPowerTools(从 NuGet 安装)对 .docx Word 文档上的文本进行基本搜索和替换。我已经遵循了该网站和他的博客上的示例,但由于某种原因,当我在运行代码后打开文档时,我从未看到文档中出现的更改。这是我正在运行的简单函数:
void ReadDocument(string path)
{
using (WordprocessingDocument doc = WordprocessingDocument.Open(path, true))
{
var content = doc.MainDocumentPart.GetXDocument().Descendants(W.p);
var regex = new Regex(@"the", RegexOptions.IgnoreCase);
int count = OpenXmlRegex.Replace(content, regex, "NewText", null);
doc.MainDocumentPart.Document.Save();
doc.Save();
MessageBox.Show(count.ToString());
}
}
Run Code Online (Sandbox Code Playgroud)
消息框确实显示了它应该进行的大量替换,但是当我打开文档时,我没有看到任何替换。另外,我认为我不需要这些文档 .Save() 调用,但我一直在尝试任何方法来让这个东西工作。有什么建议么?谢谢
我有一张 Microsoft Word 表格。我需要在表格的一行中合并两个单元格。我得到了我需要的细胞:
Wordprocessing.TableRow row = table.Elements<Wordprocessing.TableRow>().ElementAt(i);
Wordprocessing.TableCell cell1 = row.Elements<Wordprocessing.TableCell>().ElementAt(j);
Wordprocessing.TableCell cell2 = row.Elements<Wordprocessing.TableCell>().ElementAt(j+1);
Run Code Online (Sandbox Code Playgroud)
如何水平合并这些单元格?
如何使用 C# .Net 在 OpenXML 中添加条件格式。我希望适用以下条件:
=INDIRECT("D"&ROW())="Disapproved" 那么规则应该适用于:=$1:$3,$N$4:$XFD$4,$5:$1048576
我的功能设置如下:
using (SpreadsheetDocument document = SpreadsheetDocument.Open(openFileDialog1.FileName, true))
{
// apply conditions here
}
Run Code Online (Sandbox Code Playgroud)