


Detailed introduction to Java's implementation of merging cells and exporting excel sample code
这篇文章主要给大家介绍了关于java实现合并单元格的同时并导出excel的相关资料,文中先进行了简单的介绍,之后给出了详细的示例代码,相信对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
介绍
POI提供API给Java程序对Microsoft Office格式档案读和写的功能。POI可以操作的文档格式有excel,word,powerpoint等,POI进行跨行需要用到对象HSSFSheet对象,现在就当我们程序已经定义了一个HSSFSheet对象sheet。
跨第1行第1个到第2个单元格的操作为
sheet.addMergedRegion(new Region(0,(short)0,0,(short)1));
跨第1行第1个到第2行第1个单元格的操作为
sheet.addMergedRegion(new Region(0,(short)0,1,(short)0));
重点注意事项:
1.单元格CELL和ROW对象下标都是从0开始的。
2.单元格合并时Region(1,2,3,4)第1个值的行号必须要比3位置的行号小,如果大于3就不能正常合并单元格
3.合并单元格的时候要合并的单单元格必须先创建,这样方便后面再次获取这个单元格来填充数据,主要就是因为合并时不能由后向前进行合并引起的。
示例代码
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); /***这里是问题的关键,将这个工作簿写入到一个流中就可以输出相应的名字,这里需要写路径就ok了。 FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close(); **/ /**第二种是输出到也面中的excel名称 * pName="栏目统计表"; response.reset(); response.setContentType("application/x-msdownload"); response.setHeader("Content-Disposition","attachment; filename="+new String(pName.getBytes("gb2312"),"ISO-8859-1")+".xls"); ServletOutputStream outStream=null; try{ outStream = response.getOutputStream(); wb.write(outStream); }catch(Exception e) { e.printStackTrace(); }finally{ outStream.close(); } * */ System.out.print("OK"); } catch (Exception ex) { ex.printStackTrace(); } } }
总结
The above is the detailed content of Detailed introduction to Java's implementation of merging cells and exporting excel sample code. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
