1.java操作word 的有哪几种方式
java读取word文档时,虽然网上介绍了很多插件poi、java2Word、jacob、itext等等,poi无法读取格式(新的API估计行好像还在处于研发阶段,不太稳定,做项目不太敢用);java2Word、jacob容易报错找不到注册,比较诡异,我曾经在不同的机器上试过,操作方法完全一致,有的机器不报错,有的报错,去他们论坛找高人解决也说不出原因,项目部署用它有点玄;itxt好像写很方便但是我查了好久资料没有见到过关于读的好办法。
经过一番选择还是折中点采用rtf最好,毕竟rtf是开源格式,不需要借助任何插件,只需基本IO操作外加编码转换即可。rtf格式文件表面看来和doc没啥区别,都可以用word打开,各种格式都可以设定。
----- 实现的功能:读取rtf模板内容(格式和文本内容),替换变化部分,形成新的rtf文档。----- 实现思路:模板中固定部分手动输入,变化的部分用$info$表示,只需替换$info$即可。
1、采用字节的形式读取rtf模板内容2、将可变的内容字符串转为rtf编码3、替换原文中的可变部分,形成新的rtf文档主要程序如下:public String bin2hex(String bin) {char[] digital = "0123456789ABCDEF".toCharArray();StringBuffer sb = new StringBuffer("");byte[] bs = bin.getBytes();int bit;for (int i = 0; i < bs.length;i++)="" {bit="(bs[i]" &="" 0x0f0)="">> 4;sb.append("\\'");sb.append(digital[bit]);bit = bs[i] & 0x0f;sb.append(digital[bit]);}return sb.toString(); }public String readByteRtf(InputStream ins, String path){ String sourcecontent =""; try{ ins = newFileInputStream(path); byte[] b= new byte[1024];if (ins == null) {System.out.println("源模板文件不存在");}int bytesRead = 0;while (true) {bytesRead = ins.read(b, 0, 1024); // return final read bytescountsif(bytesRead == -1) {// end of InputStreamSystem.out.println("读取模板文件结束");break;}sourcecontent += new String(b, 0, bytesRead); // convert to stringusing bytes} }catch(Exception e){ e.printStackTrace(); }。
2.有用aspose.words的吗,请教单元格内容怎么水平居中
使用Aspose.Cells控件来操作Excel,然后可以使用Aspose.Cells.Style来设置Excel单元格的属性,设置单元格的背景颜色,字体大小等。
Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook();
//为单元格添加样式
Aspose.Cells.Style style = wb.Styles[wb.Styles.Add()];
//设置居中
style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;
//设置背景颜色
style.ForegroundColor = System.Drawing.Color.FromArgb(153, 204, 0);
style.Pattern = AC.BackgroundType.Solid;
style.Font.IsBold = true;
3.
我有同样问题,添加引用后出现警告,但安装试用版没问题, 当前的目标框架“.NETFramework,Version=v4.0,Profile=Client”未包含引用的程序集“Aspose.Words, Version=11.7.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56, processorArchitecture=MSIL”所依赖的“System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”。
这会导致无法解析引用的程序集。若要解决此问题,请 (1) 更改此项目的目标框架,或者 (2) 从此项目中删除引用的程序集。
4.如何使用Aspose Words for Java插入条形图
虽然Aspose.Words for Java目前还不允许在Word文档中创建条形图。但是可以通过Aspose.Cells for Java创建静态条形图,并选染成图片,然后再通过Aspose.Words for Java添加到Word文档中:
//Create a new Workbook.
Workbook workbook = new Workbook();
//Get the first worksheet.
Worksheet sheet = workbook.getWorksheets().get(0);
//Set the name of worksheet
sheet.setName("Data");
//Get the cells collection in the sheet.
Cells cells = workbook.getWorksheets().get(0).getCells();
//Put some values into a cells of the Data sheet.
cells.get("A1").setValue("Region");
cells.get("A2").setValue("France");
cells.get("A3").setValue("Germany");
cells.get("A4").setValue("England");
5.如何使用Aspose.Words将RichText嫖猈ord
public override bool GetValue(string fieldName, out object fieldValue) { 。
}所以我把当前字段的字段名放到Word模板中,并且设置fieldValue为一个字符串,这样fieldValue的字符串就可以出现在Word文档中。但是当我设置了一个FlowDocument fieldValue,结果将是一个XML字符串(ToString表示的是FlowDocument对象)在 Aspose.Words的帮助文档中,我虽然没有找到任何关于所需类型的fieldValue参数。
但是可以通过如下方法加载富文本到Aspose.Words文档对象中:private class HandleMergeFields : IFieldMergingCallback{void IFieldMergingCallback.FieldMerging(FieldMergingArgs e){DocumentBuilder builder = new DocumentBuilder(e.Document);builder.MoveToMergeField("fieldName");Node node = builder.CurrentNode;// doc is an RTF document we created from RTF stringInsertDocument(node, doc);。
6.请问如何使用Aspose.Words将文件转换为MHTML和Email
C# // Load the document into Aspose.Words. string srcFileName = Path.Combine(dataDir, "DinnerInvitationDemo.doc"); Document doc = new Document(srcFileName); // Save into a memory stream in MHTML format. Stream stream = new MemoryStream(); doc.Save(stream, SaveFormat.Mhtml); // Rewind the stream to the beginning so Aspose.Network can read it. stream.Position = 0; // Create an Aspose.Network MIME email message from the stream. MailMessage message = MailMessage.Load(stream, MessageFormat.Mht); message.From = "your_from@email.com"; message.To = "your_to@email.com"; message.Subject = "Aspose.Words + Aspose.Network MHTML Test Message"; // Send the message using Aspose.Network SmtpClient client = new SmtpClient(); client.Host = "your_smtp.com"; client.AuthenticationMethod = SmtpAuthentication.None; client.Send(message); VB ' Load the document into Aspose.Words. Dim srcFileName As String = Path.Combine(dataDir, "DinnerInvitationDemo.doc") Dim doc As New Document(srcFileName) ' Save into a memory stream in MHTML format. Dim stream As Stream = New MemoryStream() doc.Save(stream, SaveFormat.Mhtml) ' Rewind the stream to the beginning so Aspose.Network can read it. stream.Position = 0 ' Create an Aspose.Network MIME email message from the stream. Dim message As MailMessage = MailMessage.Load(stream, MessageFormat.Mht) message.From = "your_from@email.com" message.To = "your_to@email.com" message.Subject = "Aspose.Words + Aspose.Network MHTML Test Message" ' Send the message using Aspose.Network Dim client As New SmtpClient() client.Host = "your_smtp.com" client.AuthenticationMethod = SmtpAuthentication.None client.Send(message)。
转载请注明出处51数据库 » apose.words插件使用