用户登录
用户注册

分享至

poiword不换行

  • 作者: 大胃贝壳汉姆
  • 来源: 51数据库
  • 2020-04-21

1.java poi操作2007word,输入什么代表换行符

悄悄问了问度娘,她说按下面方式做就ok了:

1. 首先在需要强制换行的单元格里使用poi的样式,并且把样式设定为自动换行

# HSSFCellStyle cellStyle=workbook.createCellStyle();

# cellStyle.setWrapText(true);

# cell.setCellStyle(cellStyle);

2. 其次是在需要强制换行的单元格,使用\就可以实再强制换行

1. HSSFCell cell = row.createCell((short)0);

2. cell.setCellStyle(cellStyle); cell.setCellValue(new HSSFRichTextString("hello\r\n world!"));

这样就能实现强制换行,

换行后的效里是单元格里强制换行

hello

world!

2.poi生成表格不能自动换行

POI生成excel表格,如何合并单元格Java代码import java.io.FileOutputStream;import java.io.IOException;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.hssf.util.Region;public class ExcelTest {/*** @param args*/public static void main(String[] args) throws IOException {try {HSSFWorkbook wb = new HSSFWorkbook();HSSFSheet sheet = wb.createSheet("new sheet");HSSFCellStyle style = wb.createCellStyle(); // 样式对象style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平HSSFRow row = sheet.createRow((short) 0);HSSFRow row2 = sheet.createRow((short) 1);sheet.addMergedRegion(new Region(0, (short) 0, 1, (short) 0));HSSFCell ce = row.createCell((short) 0);ce.setEncoding(HSSFCell.ENCODING_UTF_16);// 中文处理ce.setCellValue("项目\\日期"); // 表格的第一行第一列显示的数据ce.setCellStyle(style); // 样式,居中int num = 0;for (int i = 0; i < 9;="" i++)="" {="" 循环9次,每一次都要跨单元格显示//="" 计算从那个单元格跨到那一格int="" celln="0;int" celle="0;if" (i="=" 0)="" {celln="0;celle" =="" 1;}="" else="" {celln="(i" *="" 2);celle="(i" *="" 2="" +="" 1);}//="" 单元格合并//="" 四个参数分别是:起始行,起始列,结束行,结束列sheet.addmergedregion(new="" region(0,="" (short)="" (celln="" +="" 1),="" 0,(short)="" (celle="" +="" 1)));hssfcell="" cell="row.createCell((short)" (celln="" +="" 1));cell.setcellvalue("merging"="" +="" i);="" 跨单元格显示的数据cell.setcellstyle(style);="" 样式//="" 不跨单元格显示的数据,如:分两行,上一行分别两格为一格,下一行就为两格,“数量”,“金额”hssfcell="" cell1="row2.createCell((short)" celle);hssfcell="" cell2="row2.createCell((short)" (celle="" +="" 1));cell1.setencoding(hssfcell.encoding_utf_16);cell1.setcellvalue("数量");cell1.setcellstyle(style);cell2.setencoding(hssfcell.encoding_utf_16);cell2.setcellvalue("金额");cell2.setcellstyle(style);num++;}//="" 在后面加上合计百分比//="" 合计="" 在最后加上,还要跨一个单元格sheet.addmergedregion(new="" region(0,="" (short)="" (2="" *="" num="" +="" 1),="" 0,(short)="" (2="" *="" num="" +="" 2)));hssfcell="" cell="row.createCell((short)" (2="" *="" num="" +="" 1));cell.setencoding(hssfcell.encoding_utf_16);cell.setcellvalue("合计");cell.setcellstyle(style);hssfcell="" cell1="row2.createCell((short)" (2="" *="" num="" +="" 1));hssfcell="" cell2="row2.createCell((short)" (2="" *="" num="" +="" 2));cell1.setencoding(hssfcell.encoding_utf_16);cell1.setcellvalue("数量");cell1.setcellstyle(style);cell2.setencoding(hssfcell.encoding_utf_16);cell2.setcellvalue("金额");cell2.setcellstyle(style);//="" 百分比="" 同上sheet.addmergedregion(new="" region(0,="" (short)="" (2="" *="" num="" +="" 3),="" 0,(short)="" (2="" *="" num="" +="" 4)));hssfcell="" cellb="row.createCell((short)" (2="" *="" num="" +="" 3));cellb.setencoding(hssfcell.encoding_utf_16);cellb.setcellvalue("百分比");cellb.setcellstyle(style);hssfcell="" cellb1="row2.createCell((short)" (2="" *="" num="" +="" 3));hssfcell="" cellb2="row2.createCell((short)" (2="" *="" num="" +="" 4));cellb1.setencoding(hssfcell.encoding_utf_16);cellb1.setcellvalue("数量");cellb1.setcellstyle(style);cellb2.setencoding(hssfcell.encoding_utf_16);cellb2.setcellvalue("金额");cellb2.setcellstyle(style);fileoutputstream="" fileout="new" fileoutputstream("workbook.xls");wb.write(fileout);fileout.close();system.out.print("ok");}="" catch="" (exception="" ex)="">

3.java用poi往excel里写数据遇到换行问题

String[] arr = new String[] { "a", "b", "c", "d", "a", "b", "c", "d", "a", "b", "c", "d" };

List<String> list = Arrays.asList(arr);

if (list != null) {

StringBuffer buff = new StringBuffer();

int colIndex = 0;// 用来标识列下标

for (int i = 0; i < list.size(); i++) {

if (i != 0 && i % 3 == 0) {// 不是第一个下标并且能被3整除,也就是4个一个轮回

buff.append(list.get(i));

rows.createCell(colIndex++).setCellValue(new String(buff));

buff = new StringBuffer();// 清空buff内容

} else {

buff.append(list.get(i));

}

}

}

4.java poi 怎么控制输出word每行的内容

你好,试试以下代码行不行。

package com.sample; import java.awt.Color; import java.io.FileOutputStream; import java.io.IOException; import com.lowagie.text.Cell; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Element; import com.lowagie.text.Font; import com.lowagie.text.FontFactory; import com.lowagie.text.Image; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.Table; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.rtf.RtfWriter2; /** * * @author wangyanjun * @email bd_wyj@sina.com * @createDate Jun 12, 2008 */ public class CreateWordDemo { public void createDocContext(String file) throws DocumentException, IOException { // 设置纸张大小 Document document = new Document(PageSize.A4); // 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中 RtfWriter2.getInstance(document, new FileOutputStream(file)); document.open(); // 设置中文字体 BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); // 标题字体风格 Font titleFont = new Font(bfChinese, 12, Font.BOLD); // 正文字体风格 Font contextFont = new Font(bfChinese, 10, Font.NORMAL); Paragraph title = new Paragraph("标题"); // 设置标题格式对齐方式 title.setAlignment(Element.ALIGN_CENTER); title.setFont(titleFont); document.add(title); String contextString = "iText是一个能够快速产生PDF文件的java类库。" + " \n"// 换行 + "iText的java类对于那些要产生包含文本," + "表格,图形的只读文档是很有用的。

它的类库尤其与java Servlet有很好的给合。" + "使用iText与PDF能够使你正确的控制Servlet的输出。

"; Paragraph context = new Paragraph(contextString); // 正文格式左对齐 context.setAlignment(Element.ALIGN_LEFT); context.setFont(contextFont); // 离上一段落(标题)空的行数 context.setSpacingBefore(5); // 设置第一行空的列数 context.setFirstLineIndent(20); document.add(context); //利用类FontFactory结合Font和Color可以设置各种各样字体样式 /** * Font.UNDERLINE 下划线,Font.BOLD 粗体 */ Paragraph underline = new Paragraph("下划线的实现", FontFactory.getFont( FontFactory.HELVETICA_BOLDOBLIQUE, 18, Font.UNDERLINE, new Color(0, 0, 255))); document.add(underline); // 设置 Table 表格 Table aTable = new Table(3); int width[] = {25,25,50}; aTable.setWidths(width);//设置每列所占比例 aTable.setWidth(90); // 占页面宽度 90% aTable.setAlignment(Element.ALIGN_CENTER);//居中显示 aTable.setAlignment(Element.ALIGN_MIDDLE);//纵向居中显示 aTable.setAutoFillEmptyCells(true); //自动填满 aTable.setBorderWidth(1); //边框宽度 aTable.setBorderColor(new Color(0, 125, 255)); //边框颜色 aTable.setPadding(2);//衬距,看效果就知道什么意思了 aTable.setSpacing(3);//即单元格之间的间距 aTable.setBorder(2);//边框 //设置表头 /** * cell.setHeader(true);是将该单元格作为表头信息显示; * cell.setColspan(3);指定了该单元格占3列; * 为表格添加表头信息时,要注意的是一旦表头信息添加完了之后, * 必须调用 endHeaders()方法,否则当表格跨页后,表头信息不会再显示 */ Cell haderCell = new Cell("表格表头"); haderCell.setHeader(true); haderCell.setColspan(3); aTable.addCell(haderCell); aTable.endHeaders(); Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.GREEN); Cell cell = new Cell(new Phrase("这是一个测试的 3*3 Table 数据", fontChinese )); cell.setVerticalAlignment(Element.ALIGN_TOP); cell.setBorderColor(new Color(255, 0, 0)); cell.setRowspan(2); aTable.addCell(cell); aTable.addCell(new Cell("#1")); aTable.addCell(new Cell("#2")); aTable.addCell(new Cell("#3")); aTable.addCell(new Cell("#4")); Cell cell3 = new Cell(new Phrase("一行三列数据", fontChinese )); cell3.setColspan(3); cell3.setVerticalAlignment(Element.ALIGN_CENTER); aTable.addCell(cell3); document.add(aTable); document.add(new Paragraph("\n")); //添加图片 Image img=Image.getInstance("d:\\img01800.jpg"); img.setAbsolutePosition(0, 0); img.setAlignment(Image.RIGHT);//设置图片显示位置 img.scaleAbsolute(12,35);//直接设定显示尺寸 img.scalePercent(50);//表示显示的大小为原尺寸的50% img.scalePercent(25, 12);//图像高宽的显示比例 img.setRotation(30);//图像旋转一定角度 document.add(img); document.close(); } /** * @param args */ public static void main(String[] args) { CreateWordDemo word = new CreateWordDemo(); String file = "c:/demo1.doc"; 。

5.JAVA使用POI读写word 乱码

如何使用java、poi读写word文档??能不能将一个word的内容完全读过来,放到一个新生成的word文件中去,要求能将word中的表格、图片等保留,格式不变。

最好能给个例子?网上多是很早以前的那个解决方法如下:,只能读文本内容,且新生成的word文件打开时总是要提示选择编码,不太好用,希望能有新的解决方案??!!poi操作word1.1 添加poi支持:包下载地址1.2 poi对excel文件的读取操作比较方便,poi还提供对word的doc格式文件的读取。但在它的发行版本中没有发布对word支持的模块,需要另外下载一个poi的扩展的jar包。

下载地址为;下载extractors-0.4_zip这个文件2、提取doc文件内容 public static string readdoc(string doc) throws exception {// 创建输入流读取doc文件 fileinputstream in = new fileinputstream(new file(doc)); wordextractor extractor = null; string text = null;// 创建wordextractor extractor = new wordextractor();// 对doc文件进行提取 text = extractor.extracttext(in); return text; } public static void main(string[] args) { try{ string text = wordreader.readdoc("c:/test.doc"); system.out.println(text); }catch(exception e){ e.printstacktrace(); } }3、写入doc文档 import java.io.bytearrayinputstream; import java.io.fileoutputstream; import java.io.ioexception; import org.apache.poi.poifs.filesystem.directoryentry; import org.apache.poi.poifs.filesystem.documententry; import org.apache.poi.poifs.filesystem.poifsfilesystem; public class wordwriter { public static boolean writedoc(string path, string content) { boolean w = false; try { // byte b[] = content.getbytes("iso-8859-1"); byte b[] = content.getbytes(); bytearrayinputstream bais = new bytearrayinputstream(b); poifsfilesystem fs = new poifsfilesystem(); directoryentry directory = fs.getroot(); documententry de = directory.createdocument("worddocument", bais); fileoutputstream ostream = new fileoutputstream(path); fs.writefilesystem(ostream); bais.close(); ostream.close(); } catch (ioexception e) { e.printstacktrace(); } return w; } public static void main(string[] args) throws exception{ string wr=wordreader.readdoc("d:\\test.doc"); boolean b = writedoc("d:\\result.doc",wr);。

转载请注明出处51数据库 » poiword不换行

软件
前端设计
程序设计
Java相关