asp.net azure中的文字自动化 [英] word automation in asp.net azure
问题描述
嗨我正在使用windows azure blob 1.我已经上传了一个单词模板文件
2.我想创建一个带有模板文档的新文件里面我想要q\write一些内容
i我正在使用这段代码,但你得到错误PLZ任何帮助我.....非常好
string destinationFile = DateTime.Now.ToString(yyyyMMddHHmmss_)+sample.doc;
string sourceFile = ERS_Sales_Quotation_new.dotx;
ProSpector.Core.ImageUtilities obj = new ProSpector.Core.ImageUtilities();
// destinationFile = obj.CopyBlobFile(sourceFile,destinationFile);
string ContainerURL = ProSpector.BlobStorage.GetContainer()。Uri.ToString()+/;
var blob = ProSpector.BlobStorage.GetContainer()。GetBlobReference(sourceFile);
MemoryStream memStream = new MemoryStream (blob.DownloadByteArray());
memStream.Seek(0,SeekOrigin.Begin);
//blob.DownloadToStream(memStream);
// ContainerURL + destinationFile
//blob.DownloadToFile(ContainerURL + destinationFile);
DataTable dt = new DataTable(); //假设数据来自数据库
dt.Columns.Add(ID);
dt.Columns.Add(Name );
dt.Columns.Add(性别);
dt.Rows.Add(1,Tom,male);
dt.Rows.Add(2,Jim,male);
dt.Rows.Add(3,LiSa,female);
dt.Rows.Add(4,LiLi,female);
for(int we = 0;我们< 1000;我们++)
{
dt.Rows.Add(我们,LiLi,女性);
}
使用(WordprocessingDocument wordDoc = WordprocessingDocument.Open(memStream,true))
{
wordDoc.ChangeDocumentType(DocumentFormat.OpenXml。 WordprocessingDocumentType.Document);
var mainPart = wordDoc.MainDocumentPart;
DocumentSettingsPart documentSettingPart1 = mainPart.DocumentSettingsPart ;
//创建一个新的attachmentTemplate并指定一个关系ID
AttachedTemplate attachedTemplate1 = new AttachedTemplate(){Id =relationId1};
//将附加的模板附加到DocumentSettingsPart
documentSettingPart1.Settings.Append(attachedTemplate1);
//添加AttachedTemplate类型的ExternalRelationShip。
//指定模板的路径和关系ID
documentSettingPart1.AddExternalRelationship(http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate ,新的Uri(https://prospectorstorage.blob.core.windows.net/template/+ sourceFile,UriKind.Absolute),relationId1);
var res = from mainPart中的mark .Document.Body.Descendants< bookmarkstart>()
其中mark.Name ==QuotationBookmark
选择标记;
var bookmark = res.SingleOrDefault();
if(bookmark!= null)
{
var parent = bookmark.Parent;
DocumentFormat.OpenXml.Wordprocessing.Table table = new DocumentFormat.OpenXml.Wordprocessing.Table();
TableProperties tblProp = new DocumentFormat.OpenXml.Wordprocessing.TableProperties(
new TableBorders(
new Border()
{
Val = new DocumentFormat.OpenXml.EnumValue< bordervalues>(BorderValues.DotDash),
尺寸= 24
}
)
);
table.AppendChild< tableproperties>(tblProp);
DocumentFormat.OpenXml.Wordprocessing.TableRow trHead = new DocumentFormat.OpenXml.Wordprocessing.TableRow();
if(trHead.TableRowProperties == null)
trHead.TableRowProperties = new TableRowProperties();
trHead.TableRowProperties.AppendChild (new TableHeader());
for(int j = 0; j< dt.Columns.Count; j ++)
{
DocumentFormat.OpenXml.Wordprocessing.TableCell tc = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
tc.Append (new TableCellProperties(new TableCellWidth(){Type = TableWidthUnitValues.Dxa,Width =240,}));
tc.Append(new Paragraph(new Run(new Text(dt.Columns [ j] .ColumnName))));
trHead.Append(tc);
}
table.Append(trHead);
$ i $ 0; i< dt.Rows.Count; i ++)
$
DocumentFormat .OpenXml.Wordprocessing.TableRow tr = new DocumentFormat.OpenXml.Wordprocessing.TableRow();
for(int j = 0; j< dt.Columns.Count; j ++)
{
DocumentFormat.OpenXml.Wordprocessing.TableCell tc = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
tc.Append(new TableCellProperties(new TableCellWidth(){Type = TableWidthUnitValues.Dxa,Width =240 });
tc.Append(new Paragraph(new Run(new Text(dt.Rows [i] [j] .ToString()))));
tr.Append(tc);
}
table.Append(tr);
}
parent.InsertAfterSelf(table);
}
mainPart.Document.Save();
// var Destblob = ProSpector.BlobStorage.GetContainer() .GetBlobReference(destinationFile);
// CloudBlob blob = BlobContainer.GetB lobReference(fileName);
// Destblob.Properties.ContentType =application / vnd.openxmlformats-officedocument.wordprocessingml.document;
memStream.Position = 0;
memStream.WriteTo(Response.OutputStream);
var Destblob = ProSpector.BlobStorage.GetContainer()。GetBlobReference(destinationFile);
Destblob。 Properties.ContentType =application / vnd.openxmlformats-officedocument.wordprocessingml.document;
//Destblob.SetProperties();
BlobRequestOptions options = new BlobRequestOptions();
options.AccessCondition = AccessCondition.None;
Destblob.UploadFromStream(memStream,options); < br $>
// Label1.Text = Destblob.Uri.ToString();
//返回blob.Uri.ToString();
// blob.UploadFile(ContainerURL + destinationFile);
// blob.Properties.ContentType = ProSpector.Core.Utilities.GetFileContentType(doc);
// blob.SetProperties();
// BlobRequestOptions options = new BlobRequestOptions();
// options.AccessCondition = AccessCondition.None;
}
Label1.Text = destinationFile;
}
推荐答案
你没告诉我们你得到了什么错误 - 我们无法复制错误或嗅出错误。你可以通过编程方式添加内容来实现这一目标,但我认为你应该选择基于模板的方法。我建议你看一下.NET的这个工具包,它正是你想要实现的目标。
You didn't tell us what error you are getting - there is no way for us to replicate the error or sniff it it out. You can definitely achieve this by adding content programmatically, but I think you should choose a template based approach. I suggest you to take a look at this toolkit for .NET which does exactly what you are trying to achieve.
这篇关于asp.net azure中的文字自动化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!