我目前使用这样的东西在单元格中插入内联字符串:
new Cell()
{
CellReference = "E2",
StyleIndex = (UInt32Value)4U,
DataType = CellValues.InlineString,
InlineString = new InlineString(new Text( "some text"))
}
Run Code Online (Sandbox Code Playgroud)
但是\n插入换行符不起作用,我该怎么做呢?
谢谢
响应
new Cell(
new CellValue("string \n string")
)
{
CellReference = "E2",
StyleIndex = (UInt32Value)4U,
DataType = CellValues.String
}
Run Code Online (Sandbox Code Playgroud) 是否可以使用Open XML sdk来操作当前在Office应用程序中打开的文档部分(word/ppt).我知道最简单的方法是使用VSTO,但它的速度很慢并且会使用剪贴板来插入元素,OXML sdk是直接且简单的.
如果有人可以发布一些很棒的代码示例.
在此先感谢
Rakesh
我花了很多时间试图找出一种使用OpenXml 2.0将任何文件嵌入Microsoft Word的好方法; Office文档相当容易,但其他文件类型如PDF,TXT,GIF,JPG,HTML等等.
在C#中使用任何文件类型的好方法是什么?
我正在使用此代码的修改版本来创建Excel文档,添加单元格和样式单元格.我正在尝试修改此代码,以便能够将图像添加到工作表.我无处可去,网上真的没什么可以帮助的.我试图通读OpenXML生产力工具差异.这有点无益.这里有人能指出我正确的方向吗?
谢谢.
SdtCell并且SdtBlock在OpenXml.Wordprocessing两者序列化中都有<w:sdt>什么区别?我假设一个是表格单元格,这是Microsoft文档似乎暗示的内容.提供的详细信息最多,请澄清.
如何在OpenXml中设置一行(或整行)中几个单元格的背景?
阅读了几篇文章:
我仍然无法使它工作.
我的任务实际上乍一看似乎有点容易,与这些文章中的内容略有不同.上述教程主要展示了如何创建新文档并对其进行样式化.虽然我需要改变现有的样式.
也就是说,我有一个现有的xlsx文档(报告模板).我使用必要的值填充报告(由于SO open xml excel读取单元格值和MSDN使用工作表(Open XML SDK)而设法执行此操作).但接下来我需要用红色背景标记几行.
我不确定是否要使用,CellStyle或者是否应该使用CellFormat或其他东西......这就是我现在所做的:
SpreadsheetDocument doc = SpreadsheetDocument.Open("ole.xlsx", true);
Sheet sheet = (Sheet)doc.WorkbookPart
.Workbook
.Sheets
.FirstOrDefault();
WorksheetPart worksheetPart = (WorksheetPart)doc.WorkbookPart
.GetPartById(sheet.Id);
Worksheet worksheet = worksheetPart.Worksheet;
CellStyle cs = new CellStyle();
cs.Name = StringValue.FromString("Normal");
cs.FormatId = 0;
cs.BuiltinId = 0;
//where are the style values?
WorkbookStylesPart wbsp = doc.WorkbookPart
.GetPartsOfType<WorkbookStylesPart>()
.FirstOrDefault();
wbsp.Stylesheet.CellStyles.Append(cs);
wbsp.Stylesheet.Save();
Cell cell = GetCell(worksheet, "A", 20);
cell.StyleIndex …Run Code Online (Sandbox Code Playgroud) 打开一个包含表格的Word(2007/2010)文档,选择表格并右键单击,选择AutoFit - > AutoFit to Window
如何使用OpenXML SDK 2.5在C#中实现此操作?
我有一个来自OpenXML的MemoryStream的问题.我成功打开一个Word文件,更改它并通过HttpResponse下载它,如果我在一个方法中执行所有步骤.
但是如果我尝试通过返回MemoryStream在两个不同的类(或方法)中执行它,我会得到一个损坏的word文件.我想到了冲洗或缓冲问题,但我没有找到解决方案.
这是工作代码:
public void FillTemplateOpenXmlWord(HttpResponse response)
{
string filePath = @"c:\template.docx";
byte[] filebytes = File.ReadAllBytes(filePath);
using (MemoryStream stream = new MemoryStream(filebytes))
{
using (WordprocessingDocument myDoc = WordprocessingDocument.Open(stream, true))
{
// do some changes
...
myDoc.MainDocumentPart.Document.Save();
}
string docx = "docx";
response.Clear();
response.ClearHeaders();
response.ClearContent();
response.AddHeader("content-disposition", "attachment; filename=\"" + docx + ".docx\"");
response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
response.ContentEncoding = Encoding.GetEncoding("ISO-8859-1");
stream.Position = 0;
stream.CopyTo(response.OutputStream);
response.End();
}
}
Run Code Online (Sandbox Code Playgroud)
这是非工作代码:
public void OpenFile(HttpResponse response)
{
MemoryStream stream = this.FillTemplateOpenXmlWord();
string docx = "docx";
response.Clear();
response.ClearHeaders(); …Run Code Online (Sandbox Code Playgroud)
我在.NETCoreApp 1.1中使用OpenXml 2.7.2生成了一个excel文件,并将其保存到项目文件夹中.然后在我的函数中,我读取字节并尝试将其作为文件返回.我没有收到错误,但在回复中我只是得到了二进制文件.所以它似乎工作,但没有下载.
这是我的代码:
[HttpGet]
[Route("export")]
public IActionResult Export()
{
return File(System.IO.File.ReadAllBytes(filePath),
contentType: "application/octet-stream",
fileDownloadName: "MySheet.xlsx");
}
Run Code Online (Sandbox Code Playgroud)
如果有人知道如何在.net核心中提供可下载的excel文件,请告诉我.任何帮助表示赞赏谢谢!
UPDATE
我不确定腐败是否与使用OpenXml生成excel文件有关,因为即使我尝试导出未使用OpenXml生成的空文件,我也会收到相同的错误消息"文件已损坏且无法打开" .它也与Excel 2016不完全相关,因为我可以打开其他excel文件.
我有将一张 PowerPoint 幻灯片的内容复制到另一张幻灯片的代码。下面是如何处理图像的示例。
foreach (OpenXmlElement element in sourceSlide.CommonSlideData.ShapeTree.ChildElements.ToList())
{
string elementType = element.GetType().ToString();
if (elementType.EndsWith(".Picture"))
{
// Deep clone the element.
elementClone = element.CloneNode(true);
var picture = (Picture)elementClone;
// Get the picture's original rId
var blip = picture.BlipFill.Blip;
string rId = blip.Embed.Value;
// Retrieve the ImagePart from the original slide by rId
ImagePart sourceImagePart = (ImagePart)sourceSlide.SlidePart.GetPartById(rId);
// Add the image part to the new slide, letting OpenXml generate the new rId
ImagePart targetImagePart = targetSlidePart.AddImagePart(sourceImagePart.ContentType);
// And copy the image …Run Code Online (Sandbox Code Playgroud) openxml ×10
c# ×8
ms-word ×3
openxml-sdk ×3
excel ×2
.net ×1
.net-core ×1
cell ×1
download ×1
image ×1
line-breaks ×1
memorystream ×1
office-2007 ×1
powerpoint ×1
spreadsheet ×1