当前位置:网站首页>POI excel percentage
POI excel percentage
2022-07-03 06:53:00 【God】
The two methods :
One 、 String splicing :“20.36”+“%”,
Simple and direct ;
The disadvantage is that , Generated Excel You cannot directly create a statistical chart ( Not numbers , Manual format conversion required )
Two 、 Use POI Cell style :
// Create styles
HSSFCellStyle cellStyle2 = workbook.createCellStyle();
// Cell data format
cellStyle2.setDataFormat(workbook.createDataFormat().getFormat("0.00%"));Here is the complete code ( Unit tests can be used directly ):
@org.junit.Test
public void exportExcel() throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook();
// Set the font
HSSFFont font2 = workbook.createFont();
// Font height
font2.setFontHeightInPoints((short) 11);
// The font color
font2.setColor(HSSFFont.COLOR_NORMAL);
// Create styles
HSSFCellStyle cellStyle2 = workbook.createCellStyle();
cellStyle2.setFont(font2);
// Horizontal layout : In the middle
cellStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// Vertical center
cellStyle2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// Cell color
cellStyle2.setFillForegroundColor(HSSFColor.ORANGE.index);
// Fill cell colors
cellStyle2.setFillPattern((short) 1);
// Cell data format
cellStyle2.setDataFormat(workbook.createDataFormat().getFormat("0.00%"));
// data
int a = 99;
double b = 101;
double rate = a / b;
rate = new BigDecimal(rate).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue();
// Generate Excel
HSSFSheet sheet = workbook.createSheet("sheet");
HSSFRow row = sheet.createRow(0);
row.createCell(0).setCellValue(" Proportion ");
row.createCell(1).setCellValue(rate);
row.getCell(1).setCellStyle(cellStyle2);
// Output Excel
String filePath = "D://abc.xls";
FileOutputStream fout = new FileOutputStream(filePath);
workbook.write(fout);
fout.close();
}design sketch :

边栏推荐
- Hands on redis master-slave replication, sentinel master-slave switching, cluster sharding
- Scroll view specifies the starting position of the scrolling element
- 【LeetCode】Day93-两个数组的交集 II
- 【5G NR】UE注册流程
- pytorch练习小项目
- Chapter 8. MapReduce production experience
- Floating menu operation
- 方差迭代公式推导
- 卡特兰数(Catalan)的应用场景
- crontab定时任务
猜你喜欢

Operation principle of lua on C: Foundation

10000小时定律不会让你成为编程大师,但至少是个好的起点

The dynamic analysis and calculation of expressions are really delicious for flee

Yolov3 learning notes

Software testing assignment - day 1

2022-06-23 vgmp OSPF inter domain security policy NAT policy (under update)

2022 - 06 - 23 vgmp - OSPF - Inter - Domain Security Policy - nat Policy (Update)

Integration test practice (1) theoretical basis

2022-06-23 VGMP-OSPF-域間安全策略-NAT策略(更新中)

Example of joint use of ros+pytoch (semantic segmentation)
随机推荐
2022 CISP-PTE(三)命令执行
【开源项目推荐-ColugoMum】这群本科生基于国产深度学习框架PaddlePadddle开源了零售行业解决方案
New knowledge! The virtual machine network card causes your DNS resolution to slow down
【LeetCode】Day93-两个数组的交集 II
[classes and objects] explain classes and objects in simple terms
opencv
My 2020 summary "don't love the past, indulge in moving forward"
Heap sort and priority queue
opencv鼠标键盘事件
Practical plug-ins in idea
SSH link remote server and local display of remote graphical interface
Simple understanding of bubble sorting
DNS forward query:
Condition annotation in uni-app realizes cross segment compatibility, navigation jump and parameter transfer, component creation and use, and life cycle function
Yolov3 learning notes
2022 cisp-pte (III) command execution
Unit test notes
[LeetCode]404. Sum of left leaves
Error c2017: illegal escape sequence
Operation principle of lua on C: Foundation