我想使用开放的XML C#应用%(百分比)数字格式
我有数值3.6,我想在Excel中显示该数字为3.6%.
我如何实现这一目标?
我使用EPPlus for Excel文件生成.
我的意思是我需要将HTML文本(粗体,斜体,字体颜色,名称,大小参数)转换为Excel单元格.我想它需要创建多样式的单元格,如:
单元格文本是"你好!"
我想要的风格是:
he - bold
ll - italic
o! - red colored font
Run Code Online (Sandbox Code Playgroud)
或者(更复杂)
hello! - bold
ll - italic (also bold)
o! - red colored (also bold)
Run Code Online (Sandbox Code Playgroud)
我知道MS OpenXML库(它允许我做我需要的).这是一个很好但有点复杂的库实现.
我有大约10个word文档,我使用open xml和其他东西生成.现在我想创建另一个word文档,我想逐个加入到这个新创建的文档中.我希望使用open xml,任何提示都会很明显.以下是我的代码:
private void CreateSampleWordDocument()
{
//string sourceFile = Path.Combine("D:\\GeneralLetter.dot");
//string destinationFile = Path.Combine("D:\\New.doc");
string sourceFile = Path.Combine("D:\\GeneralWelcomeLetter.docx");
string destinationFile = Path.Combine("D:\\New.docx");
try
{
// Create a copy of the template file and open the copy
//File.Copy(sourceFile, destinationFile, true);
using (WordprocessingDocument document = WordprocessingDocument.Open(destinationFile, true))
{
// Change the document type to Document
document.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
//Get the Main Part of the document
MainDocumentPart mainPart = document.MainDocumentPart;
mainPart.Document.Save();
}
}
catch
{
}
}
Run Code Online (Sandbox Code Playgroud)
更新(使用AltChunks):
using (WordprocessingDocument myDoc = WordprocessingDocument.Open("D:\\Test.docx", true)) …Run Code Online (Sandbox Code Playgroud) 我认为2.0是更好的... ...他们有一些不错的"如何..." 的例子,但书签似乎并不为明显充当说表...一个书签被定义2个 XML元素BookmarkStart&书签结束.我们有一些带有书签的模板作为书签,我们只想用其他文本替换书签......没有奇怪的格式化,但如何选择/替换书签文字?
作为对企业级服务器端文档生成的调查的一部分,我遇到了Open XML.
谢谢
Gineer
我正在尝试使用Microsoft的OpenXML 2.5库来创建OpenXML文档.一切都很好,直到我尝试在我的文档中插入HTML字符串.我已经浏览了网页,这是我到目前为止所提出的内容(剪切到我遇到问题的部分):
Paragraph paragraph = new Paragraph();
Run run = new Run();
string altChunkId = "id1";
AlternativeFormatImportPart chunk =
document.MainDocumentPart.AddAlternativeFormatImportPart(
AlternativeFormatImportPartType.Html, altChunkId);
chunk.FeedData(new MemoryStream(Encoding.UTF8.GetBytes(ioi.Text)));
AltChunk altChunk = new AltChunk { Id = altChunkId };
run.AppendChild(new Break());
paragraph.AppendChild(run);
body.AppendChild(paragraph);
Run Code Online (Sandbox Code Playgroud)
显然,我在这个例子中并没有实际添加altChunk,但我尝试将它附加到任何地方 - 运行,段落,正文等.在任何情况下,我都无法打开Word 2010中的docx文件.
这让我有点疯狂,因为它看起来应该是直截了当的(我承认我并没有完全理解AltChunk"的事情").非常感谢任何帮助.
旁注:我发现有一件事很有意思,我不知道它是否真的是一个问题,这个响应表明AltChunk在使用MemoryStream工作时会破坏文件.任何人都能证实这是/不是真的吗?
我正在使用Microsoft Open XML SDK 2,我很难在单元格中插入日期.我可以通过设置插入数字而没有问题Cell.DataType = CellValues.Number,但是当我对日期(Cell.DataType = CellValues.Date)执行相同操作时Excel 2010崩溃(2007年也是如此).
我尝试将Cell.Text值设置为多种日期格式以及Excel的日期/数字格式无济于事.我也尝试使用样式,删除type属性,以及我扔在墙上的许多其他比萨...
有人能指出我在工作表中插入日期的例子吗?
我正在尝试使用OpenXML以xlsx格式创建Excel文件,因为我需要在Web服务器上使用它.
填写表格中的值没有任何问题; 但是我很难在单元格中设置经典的日期格式.
下面是使用DocumentFormat.OpenXml和WindowsBase引用的快速测试.
class Program
{
static void Main(string[] args)
{
BuildExel(@"C:\test.xlsx");
}
public static void BuildExel(string fileName)
{
using (SpreadsheetDocument myWorkbook =
SpreadsheetDocument.Create(fileName,
SpreadsheetDocumentType.Workbook))
{
// Workbook Part
WorkbookPart workbookPart = myWorkbook.AddWorkbookPart();
var worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
string relId = workbookPart.GetIdOfPart(worksheetPart);
// File Version
var fileVersion = new FileVersion { ApplicationName = "Microsoft Office Excel" };
// Style Part
WorkbookStylesPart wbsp = workbookPart.AddNewPart<WorkbookStylesPart>();
wbsp.Stylesheet = CreateStylesheet();
wbsp.Stylesheet.Save();
// Sheets
var sheets = new Sheets();
var sheet = …Run Code Online (Sandbox Code Playgroud) 我有一个Microsoft Word文档(docx),我使用Open XML SDK 2.0 Productivity Tool从中生成C#代码.
我想以编程方式将一些数据库值插入到文档中.为此我输入了简单的文本,如[[place holder 1]],我的程序应该用它的数据库值替换占位符.
不幸的是,XML输出处于某种混乱状态.例如,我有一个包含两个相邻单元格的表格,这些单元格不应与其占位符区分开来.但其中一个占位符分为几个版本.
[[好地方持有人]]
<w:tc xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:tcPr>
<w:tcW w:w="1798" w:type="dxa" />
<w:shd w:val="clear" w:color="auto" w:fill="auto" />
</w:tcPr>
<w:p w:rsidRPr="008C2E16" w:rsidR="001F54BF" w:rsidP="000D7B67" w:rsidRDefault="0009453E">
<w:pPr>
<w:spacing w:after="0" w:line="240" w:lineRule="auto" />
<w:rPr>
<w:rFonts w:cstheme="minorHAnsi" />
<w:sz w:val="20" />
<w:szCs w:val="20" />
</w:rPr>
</w:pPr>
<w:r w:rsidRPr="0009453E">
<w:rPr>
<w:rFonts w:cstheme="minorHAnsi" />
<w:sz w:val="20" />
<w:szCs w:val="20" />
</w:rPr>
<w:t>[[good place holder]]</w:t>
</w:r>
</w:p>
</w:tc>
Run Code Online (Sandbox Code Playgroud)
与[[坏地方持有人]]
<w:tc xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:tcPr>
<w:tcW w:w="1799" w:type="dxa" /> …Run Code Online (Sandbox Code Playgroud) 我无法将我创建的word文档流式传输到浏览器.我不断收到来自Microsoft Word的消息,该文档已损坏.
当我通过控制台应用程序运行代码并将ASP.NET从图片中删除时,文档生成正确,没有任何问题.我相信一切都围绕着写下文件.
这是我的代码:
using (MemoryStream mem = new MemoryStream())
{
// Create Document
using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(mem, WordprocessingDocumentType.Document, true))
{
// Add a main document part.
MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
new Document(new Body()).Save(mainPart);
Body body = mainPart.Document.Body;
body.Append(new Paragraph(new Run(new Text("Hello World!"))));
mainPart.Document.Save();
// Stream it down to the browser
// THIS IS PROBABLY THE CRUX OF THE MATTER <---
Response.AppendHeader("Content-Disposition", "attachment;filename=HelloWorld.docx");
Response.ContentType = "application/vnd.ms-word.document";
mem.WriteTo(Response.OutputStream);
Response.End();
}
}
Run Code Online (Sandbox Code Playgroud)
我看了很多链接 - 但没有什么可行的.我很多人使用MemoryStream.WriteTo和使用BinaryWrite …