是否可以使用Apache POI从Office 2007(xlsx/OpenXML)电子表格中提取图表信息?我已经成功阅读了电子表格,甚至获得了引用图表的部分,但不确定如何从这部分中检索任何信息,例如图表类型,图表数据等.
XSSFWorkbook xwb = new XSSFWorkbook("charts_lines.xlsx");
XSSFSheet sheet = xwb.getSheetAt(0);
Run Code Online (Sandbox Code Playgroud)
我也可以遍历包部分来检索图表部分,但我不知道如何继续检索有关图表的任何信息?
注意,我对使用POI创建图表不感兴趣,只读取尽可能多的图表信息...我也没有保存xlsx.我只想提取线条颜色,标签,数据,图表类型(饼图,线条,条形图等)
我正在使用使用Open XML SDK解析和读取大型Excel文件中提出的SAX方法从数据表创建一个大型XLSX文件.我正在使用XLSX文件作为模板.
该帖子中描述的方法可以很好地替换现有的工作表,但我想从模板中的工作表中复制标题行(字符串值,格式等),而不是仅使用标题行来自像原始代码那样的数据表.
我已经尝试了下面的代码,但XLSX文件最终在标题行中没有文本 - 格式化被复制,而不是文本.我查看了表格的XML文件,它看起来不错(引用sharedStrings.xml文件,它仍然具有字符串的定义).来自Open XML SDK 2.0 Productivity Tool的反映代码显示了一个稍微奇怪的结果:单元格似乎没有设置文本值:
cellValue1.Text = "";
Run Code Online (Sandbox Code Playgroud)
即使XML说:
<x:c r="A1" s="4" t="s">
Run Code Online (Sandbox Code Playgroud)
OpenXmlReader使用的主要代码如下:
while (reader.Read())
{
if (reader.ElementType == typeof(SheetData))
{
if (reader.IsEndElement)
continue;
// Write sheet element
writer.WriteStartElement(new SheetData());
// copy header row from template
reader.Read();
do
{
if (reader.IsStartElement)
{
writer.WriteStartElement(reader);
}
else if (reader.IsEndElement)
{
writer.WriteEndElement();
}
reader.Read();
} while (!(reader.ElementType == typeof(Row) && reader.IsEndElement));
writer.WriteEndElement();
// Write data rows
foreach (DataRow dataRow in resultsTable.Rows) …Run Code Online (Sandbox Code Playgroud) 我有这样的数据结构:
<rootnode>
<group>
<id>1</id>
<anothernode>first string</anothernode>
<anothernode>second string</anothernode>
</group>
<group>
<id>2</id>
<anothernode>third string</anothernode>
<anothernode>fourth string</anothernode>
</group>
</rootnode>
Run Code Online (Sandbox Code Playgroud)
以下代码:
EXEC sp_xml_preparedocument @index OUTPUT, @XMLdoc
SELECT *
FROM OPENXML (@index, 'rootnode/group')
WITH
(
id int 'id',
anothernode varchar(30) 'anothernode'
)
Run Code Online (Sandbox Code Playgroud)
这给了我结果
id | anothernode
————————————————
1 | first string
2 | third string
Run Code Online (Sandbox Code Playgroud)
如何在显示所有四个字符串的位置显示此结果?
id | anothernode
————————————————
1 | first string
1 | second string
2 | third string
2 | fourth string
Run Code Online (Sandbox Code Playgroud) 我创建了一个创建Excel工作簿的asp.net页面.我已设置页面设置高度和工作簿宽度的方向缩放属性,如下所示:
DocumentFormat.OpenXml.Spreadsheet.PageSetup pgOr = new DocumentFormat.OpenXml.Spreadsheet.PageSetup();
pgOr.Orientation = OrientationValues.Landscape;
pgOr.FitToHeight = 3;
pgOr.FitToWidth = 1;
newWorksheetPart.Worksheet.AppendChild(pgOr);
Run Code Online (Sandbox Code Playgroud)
但是,当您下载Excel文档并打开PageSetup时,缩放设置为1页宽和3页高,这是我想要的,但未选择"适合"单选按钮,因此它实际上并未使用这些设置.
我想在途中我需要设置一些属性为true.就像FitToPage或其他东西一样,我不知道哪一个.谁知道?
注意:请记住我使用的是DocumentFormat.OpenXml而不是Microsoft.Office.Interop.Excel.
我认为以下内容可行:
var bod = wordDoc.MainDocumentPart.Document.Body;
foreach (var bookmark in bod.Descendants<BookmarkStart>())
{
bookmark.Remove();
}
foreach (var bookmark in bod.Descendants<BookmarkEnd>())
{
bookmark.Remove();
}
Run Code Online (Sandbox Code Playgroud)
但这会破坏文件.
我正在使用Open XML,我应该更改word文件标题中的文本.要更改文档中的特定段落,我使用了以下代码:
Dim body = wdDoc.MainDocumentPart.Document.Body
Dim paras = body.Elements(Of DocumentFormat.OpenXml.Wordprocessing.Paragraph)()
Dim header = body.Elements(Of DocumentFormat.OpenXml.Wordprocessing.Header)()
For Each para In paras
For Each run In para.Elements(Of DocumentFormat.OpenXml.Wordprocessing.Run)()
For Each testo In run.Elements(Of DocumentFormat.OpenXml.Wordprocessing.Text)()
If (testo.Text.Contains("<$doc_description$>")) Then
testo.Text = testo.Text.Replace("<$doc_description$>", "replaced-text")
End If
Next
Next
Next
Run Code Online (Sandbox Code Playgroud)
提前致谢!
我一直在努力提高写入xlsx的SAX方法的性能.我知道Excel中有1048576行的限制.我只打了几次这个限制.在大多数情况下,虽然我只写出大约125K到250K行(一个大数据集).我尝试过的代码似乎没有那么快,因为它会多次写入文件.我希望有一些缓存,但似乎现在代码工作的方式有太多的磁盘访问.
下面的代码类似于使用OpenXML和SAX模板,因为我使用ClosedXML写入文件,然后切换到SAX以获取大内容.尝试对这么多行使用ClosedXML时,内存会脱离图表.这就是我使用SAX的原因.
int numCols = dt.Columns.Count;
int rowCnt = 0;
//for (curRec = 0; curRec < totalRecs; curRec++)
foreach (DataRow row in dt.Rows)
{
Row xlr = new Row();
//starting of new row.
//writer.WriteStartElement(xlr);
for (int col = 0; col < numCols; ++col)
{
Cell cell = new Cell();
CellValue v = new CellValue(row[col].ToString());
{
string objDataType = row[col].GetType().ToString();
if (objDataType.Contains(TypeCode.Int32.ToString()) || objDataType.Contains(TypeCode.Int64.ToString()))
{
cell.DataType = new EnumValue<CellValues>(CellValues.Number);
//cell.CellValue = new CellValue(row[col].ToString());
cell.Append(v);
}
else if (objDataType.Contains(TypeCode.Decimal.ToString()) || …Run Code Online (Sandbox Code Playgroud) 我正在迭代我的工作表
WorkbookPart wbPart = doc.WorkbookPart;
SharedStringTablePart sstPart = wbPart.GetPartsOfType<SharedStringTablePart>().First();
SharedStringTable sst = sstPart.SharedStringTable;
foreach (var wsp in wbPart.WorksheetParts)
{
Worksheet ws = wsp.Worksheet;
// i want to do something like this
if (ws.Name == "People_Sheet")
{
}
}
Run Code Online (Sandbox Code Playgroud)
我需要知道我正在处理哪张纸,所以我可以用不同的方式处理它.如何获取工作表的名称(当我从excel中打开它时显示)?
如果我得到一张表单,我可以通过属性找到它
doc.WorkbookPart.Workbook.Sheets.ToList().ForEach(x => Console.WriteLine(x.GetAttribute("name", "").Value));
Run Code Online (Sandbox Code Playgroud)
但是工作表和工作表之间的关系是什么?如何从工作表中获取相应的工作表或工作表名称?
更新:
所以我确实找到并尝试了如何使用OpenXML从excel表中检索Tab名称
但是,sheetName与工作表不匹配.
foreach (var wsp in wbPart.WorksheetParts)
{
Worksheet worksheet = wsp.Worksheet;
var sheetName = wbPart.Workbook.Descendants<Sheet>().ElementAt(sheetIndex).Name;
var rows = worksheet.Descendants<Row>();
...
}
Run Code Online (Sandbox Code Playgroud)
从工作表返回的行与sheetName指示的工作表中的行不对应.让我试着进一步解释
我的Excel文档中有三个工作表 - 人员,业务,产品(按此顺序)
在该循环的第一次迭代中 - 我从工作表行获得的数据是指产品表数据,但是sheetName表示"人员"
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<?mso-application progid="Word.Document"?>
<pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage">
<pkg:part pkg:name="/_rels/.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml" pkg:padding="512">
<pkg:xmlData>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml" />
</Relationships>
</pkg:xmlData>
</pkg:part>
<pkg:part pkg:name="/word/_rels/document.xml.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml" pkg:padding="256">
<pkg:xmlData>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.png" />
</Relationships>
</pkg:xmlData>
</pkg:part>
<pkg:part pkg:name="/word/document.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml">
<pkg:xmlData>
<w:document mc:Ignorable="w14 w15 wp14" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
<w:body>
<w:p w:rsidR="00AE3E50" w:rsidRDefault="00DE2072">
<w:r>
<w:drawing>
<wp:inline distT="0" distB="0" distL="0" distR="0">
<wp:extent cx="2194560" cy="1463040" />
<wp:docPr id="1" …Run Code Online (Sandbox Code Playgroud) 我正在Visual Studio中使用OXML创建Word文档。我不知道要花多长时间,我需要在文档的页脚中添加一个简单的页码。
为了生成页眉和页脚,我使用了以下方法:https : //msdn.microsoft.com/zh-cn/library/ee355228(v= office.12).aspx
据我了解,这会在我什至在文档中写任何东西之前预设默认的页眉/页脚。因此,我不确定是否可以为此添加页码?我非常感谢您的帮助,因为我一整天都被困在这里...