我有一个使用OpenXML 2构建的Excel文件,我想将其作为电子邮件附件发送.例如
System.IO.MemoryStream stream = new System.IO.MemoryStream();
SpreadsheetDocument package = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook))
AddParts(package); //created using document reflector
Run Code Online (Sandbox Code Playgroud)
使用将电子表格保存到临时文件
stream.WriteTo(new System.IO.FileStream(@"c:\test.xlsx", System.IO.FileMode.Create));
Run Code Online (Sandbox Code Playgroud)
工作良好.但是尝试直接将流作为电子邮件附件发送失败 - 当我这样做时,只需在电子邮件中附加一个空文件
System.Net.Mail.Attachment file = new System.Net.Mail.Attachment(stream, "MobileBill.xlsx", "application/vnd.ms-excel");
Run Code Online (Sandbox Code Playgroud)
有人知道怎么做吗?
我有以下代码打开Excel模板文件并将其保存为.xlsx文件,当我尝试打开新文件时,我收到以下错误.请帮忙解决这个问题.
Excel无法打开文件'sa123.xlsx',因为文件格式或扩展名无效.验证文件是否已损坏,以及文件扩展名是否与文件格式匹配.
string templateName = "C:\\temp\\sa123.xltx";
byte[] docAsArray = File.ReadAllBytes(templateName);
using (MemoryStream stream = new MemoryStream())
{
stream.Write(docAsArray, 0, docAsArray.Length); // THIS performs doc copy
File.WriteAllBytes("C:\\temp\\sa123.xlsx", stream.ToArray());
}
Run Code Online (Sandbox Code Playgroud) 我开始尝试使用适用于Microsoft Office的Open XML SDK 2.0.
我现在能够做某些事情,例如检索每张幻灯片中的所有文本,并获得演示文稿的大小.例如,我这样做:
using (var doc = PresentationDocument.Open(pptx_filename, false)) {
var presentation = doc.PresentationPart.Presentation;
Debug.Print("width: " + (presentation.SlideSize.Cx / 9525.0).ToString());
Debug.Print("height: " + (presentation.SlideSize.Cy / 9525.0).ToString());
}
Run Code Online (Sandbox Code Playgroud)
现在我想在给定的幻灯片中检索嵌入的图像.有谁知道如何做到这一点或者可以指向我关于这个主题的一些文档?
我试图找到一个可以从OpenXml生成PDF的SDK.我使用Open Xml Power Tools来转换打开的XML和html,并使用iTextSharp将Html解析为PDF.但结果是看起来很糟糕的PDF.
我还没有尝试过iText的RTF解析器.如果我朝这个方向发展,我将最终需要一个RTF转换器,使简单的转换成为一个双重的噩梦.
看起来我最终可能会编写一个基于电动工具OpenXml到HTML转换器的自定义转换器.任何建议表示赞赏.由于许可证太贵(Aspose Word/TxText),我真的在这个时候最终无法找到专业的转换器.
我想我会在调查中投入更多精力.我回到了转换实用程序"http://msdn.microsoft.com/en-us/library/ff628051.aspx"并查看了它的代码.鉴于它最大的缺点是阅读底层样式并生成样式属性.由于不处理自定义真实字体的限制,PDF看起来好多了.明天会更多调查.我希望做过这样的事情/遇到奇怪的问题,并且可以解释一些问题.
private static StringDictionary GetStyle(XElement el)
{
IEnumerable jcL = el.Elements(W.jc);
IEnumerable spacingL = el.Elements(W.spacing);
IEnumerable rPL = el.Elements(W.rPr);
StringDictionary sd = new StringDictionary();
if (HasAttribute(jcL, W.val)) sd.Add("text-align", GetAttribute(jcL, W.val));
// run prop exists
if (rPL.Count() > 0)
{
XElement r = rPL.First();
IEnumerable ftL = el.Elements(W.rFonts);
if (r.Element(W.b) != null) sd.Add("font-weight", "bolder");
if (r.Element(W.i) != null) sd.Add("font-style", "italic");
if (r.Element(W.u) != null) sd.Add("text-decoration", "underline");
if (r.Element(W.color) != null && HasAttribute(r.Element(W.color), W.val)) sd.Add("color", "#" …Run Code Online (Sandbox Code Playgroud) 正如标题所述,我试图将多个单词(.docx)文件合并为一个单词doc.这些文件中的每一篇都是一页长.我在这个实现中使用了这篇文章中的一些代码.我遇到的问题是只有第一个文档被正确编写,每个其他迭代都附加一个新文档,但文档内容与第一个相同.
这是我正在使用的代码:
//list that holds the file paths
List<String> fileNames = new List<string>();
fileNames.Add("filePath");
fileNames.Add("filePath");
fileNames.Add("filePath");
fileNames.Add("filePath");
fileNames.Add("filePath");
//get the first document
MemoryStream mainStream = new MemoryStream();
byte[] buffer = File.ReadAllBytes(fileNames[0]);
mainStream.Write(buffer, 0, buffer.Length);
using (WordprocessingDocument mainDocument = WordprocessingDocument.Open(mainStream, true))
{
//xml for the new document
XElement newBody = XElement.Parse(mainDocument.MainDocumentPart.Document.Body.OuterXml);
//iterate through eacah file
for (int i = 1; i < fileNames.Count; i++)
{
//read in the document
byte[] tempBuffer = File.ReadAllBytes(fileNames[i]);
WordprocessingDocument tempDocument = WordprocessingDocument.Open(new MemoryStream(tempBuffer), true); …Run Code Online (Sandbox Code Playgroud) 使用Word我创建了一个标准normal.dot作为测试的Docx.Hello-world级复杂度.
我希望得到all the paragraphs哪些用Word中的" Heading1"字样style.
我可以得到所有段落,但不知道如何过滤到Heading1.
using (var doc = WordprocessingDocument.Open(documentFileName, false))
{
paragraphs = doc.MainDocumentPart.Document.Body
.OfType<Paragraph>().ToList();
}
Run Code Online (Sandbox Code Playgroud) 我使用OpenXml以这种方式创建自定义powerpoint演示文稿:我在演示文稿中放置了一个关键字,我在使用OpenXml的过程中找到它并更改文本值.一切正常,但适合选项最初不起作用.文本框有选项"自动调整:溢出时缩小文本;将文本换行:打开"在我的过程之后,新文本显示在正确的位置但是自动调整没有完成,我需要单击文本框并创建一个输入以查看自动调整工作.我认为修改后只有PowerPoint检查选项.
我想要的是在流程结束时调用autofit选项.谁能帮我?
我希望你明白我想做什么.
谢谢.
我有一个以字符串形式存储在Excel中的电话号码,Excel文件已成功创建,并且数据没有错误,但是每个电话号码旁边都有一个“数字存储为文本”错误。
我已经在线阅读了我应该使用excel随附的特殊电话号码格式或自定义的000-000-0000格式的信息。我可以使用excel程序进行设置,但不能通过Java代码进行设置。
我到处寻找有关setCellType和DataFormat的信息,但是我假设CellType必须为String,除了日期以外,我看不到如何使用DataFormat。
我也看过DataFormatter,但不了解如何使用它存储数据。看来这只是为了帮助读取数据。http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/DataFormatter.html
我该怎么做以下之一?
1)将单元格标记为“忽略错误”,以忽略“存储为文本的数字”错误
2)利用内置的Excel单元格格式“特殊>电话号码”
对于1),似乎存在一个通过保存和关闭文件而保留的标志,我不知道如何使用POI编辑或查看它。有一篇关于它的文章:
Excel文档的第669页和第670页涵盖FeatFormulaErr2,该文件夹保存在FeatRecord共享功能中,从理论上讲,该功能使您可以存储以下事实:对于单元格区域,应忽略“数字作为文本”
我们还获得了两个测试文件,一个带有警告,一个带有警告-46136-NoWarnings.xls和46136-WithWarnings.xls。但是我没有创建它们!
尼克 http://mail-archives.apache.org/mod_mbox/poi-user/201003.mbox/%3C27823222.post@talk.nabble.com%3E
看来这可以在VBA中完成,cell.Errors.Item(xlNumberAsText).Ignore = True但似乎没有POI等效项
使用OpenXML,我可以按页码阅读文档内容吗?
wordDocument.MainDocumentPart.Document.Body 给出完整文档的内容.
public void OpenWordprocessingDocumentReadonly()
{
string filepath = @"C:\...\test.docx";
// Open a WordprocessingDocument based on a filepath.
using (WordprocessingDocument wordDocument =
WordprocessingDocument.Open(filepath, false))
{
// Assign a reference to the existing document body.
Body body = wordDocument.MainDocumentPart.Document.Body;
int pageCount = 0;
if (wordDocument.ExtendedFilePropertiesPart.Properties.Pages.Text != null)
{
pageCount = Convert.ToInt32(wordDocument.ExtendedFilePropertiesPart.Properties.Pages.Text);
}
for (int i = 1; i <= pageCount; i++)
{
//Read the content by page number
}
}
}
Run Code Online (Sandbox Code Playgroud)
MSDN 参考
更新1:
它看起来像分页符设置如下
<w:p w:rsidR="003328B0" w:rsidRDefault="003328B0">
<w:r>
<w:br …Run Code Online (Sandbox Code Playgroud) 我想要的是:我正在编辑一个WordprocessingDocument,并在其中添加一些跟踪的更改.这部分完成了.现在,我希望MS word 默认显示所有修订版,即,它不应要求用户单击红色侧栏以打开文档中的跟踪更改.
我做了什么:对于这一点,我发现了一类RevisionView,它增加了XML元素<w:revisionView />的settings.xml下w:settings元素.该RevisionView有一个像一些属性Comments,DisplayRevision,Formatting等我明确它们都设置为true.
RevisionView revView = new RevisionView();
revView.DisplayRevision = new OnOffValue(true);
revView.Formatting = new OnOffValue(true);
revView.InkAnnotations = new OnOffValue(true);
revView.Markup = new OnOffValue(true);
revView.Comments = new OnOffValue(true);
Run Code Online (Sandbox Code Playgroud)
然后我把它添加revView到Settings:
Settings settings = wordprocessingDocument.MainDocumentPart.DocumentSettingsPart.Settings;
settings.RemoveAllChildren<RevisionView>();
settings.AppendChild(revView);
settings.Save();
Run Code Online (Sandbox Code Playgroud)
然后我明确地检查了文档xml,它在以下内容中添加了以下xml settings:
<w:revisionView w:markup="true" w:comments="true" w:insDel="true" w:formatting="true" w:inkAnnotations="true" />
Run Code Online (Sandbox Code Playgroud)
但是在设置中添加此元素不会影响视图.它没有显示默认打开的修订版.
然后,出于测试目的,我将手中的zoom …
openxml ×10
openxml-sdk ×5
c# ×4
excel ×2
ms-word ×2
powerpoint ×2
xml ×2
.net ×1
apache-poi ×1
email ×1
excel-vba ×1
java ×1
linq ×1
vb.net ×1
vba ×1