poi excel 怎么合并单元格
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) { ex.printStackTrace(); } } }
POI生成excel表格,如何合并单元格
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) { ex.printStackTrace(); } } }
poi 导出excel 怎么设置合并的单元格自动换行?
合并单元格换行方式跟普通单元格的换行方式一样,有两种方式,一个是设置自动换行,一个是设置强制换行,下面分别介绍。
1、自动换行。
选中合并单元格,右键设置单元格格式,在对齐命令中,选中自动换行。
这样改变合并单元格的列宽,数据就跟随列宽的大小自动换行。
2、强制换行。
选中合并单元格,将光标放在需要换行的位置,按下alt+enter键,强制换行。
强制换行的数据,不会因单元格列宽的大小而改变。
用poi读取excel文件时,合并的单元格如何让每个单元格都有值
//判断表中是否含有合并单元格 public boolean hasMerged() {return sheet.getNumMergedRegions() >0 ? true : false;}// 判断指定区域内是否含有合并单元格public boolean hasMerged(Region region) {for (int row = region.getRowFrom(); row for (short col = region.getColumnFrom(); col for (int i = 0; i Region r = sheet.getMergedRegionAt(i);if (r.contains(row, col)) {return true;}}}}return false;}
poi设置word中表格的单元格样式为居中
excel表格中合并的单元格内的字居中到两行之间的位置:1、选中合并后的单元格2、在工具菜单栏选择 格式 在下拉菜单选择单元格3、在单元格格式选项卡下面选择对齐4、在文本对齐方式中水平对齐 选择居中,在垂直对齐下面选择居中。
确定。
后单元格内的字就会居中到两行之间的位置了。
JAVA利用poi如何向excel已合并的单元格中写入内容
如下:public static String do_post(String url, List name_value_pair) throws IOException {String body = "{}";DefaultHttpClient httpclient = new DefaultHttpClient();try {HttpPost httpost = new HttpPost(url);httpost.setEntity(new UrlEncodedFormEntity(name_value_pair, StandardCharsets.UTF_8));HttpResponse response = httpclient.execute(httpost);HttpEntity entity = response.getEntity();body = EntityUtils.toString(entity);} finally {httpclient.getConnectionManager().shutdown();}return body;}public static String do_get(String url) throws ClientProtocolException, IOException {String body = "{}";DefaultHttpClient httpclient = new DefaultHttpClient();try {HttpGet httpget = new HttpGet(url);HttpResponse response = httpclient.execute(httpget);HttpEntity entity = response.getEntity();body = EntityUtils.toString(entity);} finally {httpclient.getConnectionManager().shutdown();}return body;}
poi设置word中表格的单元格样式为居中
展开全部excel表格中合并的单元格内的字居中到两行之间的位置:1、选中合并后的单元格2、在工具菜单栏选择 格式 在下拉菜单选择单元格3、在单元格格式选项卡下面选择对齐4、在文本对齐方式中水平对齐 选择居中,在垂直对齐下面选择居中。
确定。
后单元格内的字就会居中到两行之间的位置了。
...
java用poi导出word文档,我要导出一个表格,表格的单元格中还要有...
有个最简单,不用使用任何 poi 或其他第三方类库的方法生成 excel ,或 word ,你把一个生成好的 word 例子用文件另存为 xml , docx 之类,然后你再直接用记事本去打开这个 docx ,你就会发现是 xml 格式,这个时候,你在程序里面就直接用普通代码生成这个 xml 就可以了。
java poi XWPFTable操作word表格的问题?
1.下载下载3.8beta4版本,请记得一定要下载该版本,其他版本读取word模板并改写内容生成新的文件后,打开新文件时会提示“word无法读取文档,文档可能损坏。
”2.集成到项目这一步很简单,只要把下载后解压得到的poi-3.8-beta4-20110826.jar和poi-scratchpad-3.8-beta4-20110826.jar两个文件复制到java web项目的lib目录下就行了3.制作word模板把需要变动的值全部用代码来代替,例如你需要改变名称的值,则可以在模板中用name来表示。
详细见附件中的doc文件。
4.调用接口方法实现对word的读写操作整个过程就是先读取模板,然后修改内容,再重新生成新的文档保存到本地或者输出文件流提供下载,下面分别是生成新文档和输出文件流两种方式的代码片断,详细的代码请见下列代码中的readwriteWord()两个重载方法。
转载请注明出处51数据库 » poi word表格合并单元格