当前位置:网站首页>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 :

边栏推荐
- Opencv mouse and keyboard events
- Machine learning | simple but feature standardization methods that can improve the effect of the model (comparison and analysis of robustscaler, minmaxscaler, standardscaler)
- 每日刷题记录 (十一)
- (翻译)异步编程:Async/Await在ASP.NET中的介绍
- These two mosquito repellent ingredients are harmful to babies. Families with babies should pay attention to choosing mosquito repellent products
- La loi des 10 000 heures ne fait pas de vous un maître de programmation, mais au moins un bon point de départ
- The essence of interview
- Practice of enterprise ab/testing platform
- RestHighLevelClient获取某个索引的mapping
- 【无标题】
猜你喜欢

Numerical method for solving optimal control problem (I) -- gradient method

Daily question brushing record (11)

Notes on the core knowledge of Domain Driven Design DDD

My 2020 summary "don't love the past, indulge in moving forward"

SQL implementation merges multiple rows of records into one row

Reading notes of "learn to ask questions"

SSH link remote server and local display of remote graphical interface

Application scenarios of Catalan number

Yolov3 learning notes

Sorting out the core ideas of the pyramid principle
随机推荐
[leetcode] day93 - intersection of two arrays II
Practical plug-ins in idea
10000小時定律不會讓你成為編程大師,但至少是個好的起點
[LeetCode]404. Sum of left leaves
golang操作redis:写入、读取kv数据
学习笔记 -- k-d tree 和 ikd-Tree 原理及对比
ssh链接远程服务器 及 远程图形化界面的本地显示
Simple understanding of bubble sorting
mysql误删root账户导致无法登录
Chapter 8. MapReduce production experience
利用C#实现Pdf转图片
EasyExcel
Modify MySQL password
Realize PDF to picture conversion with C #
These two mosquito repellent ingredients are harmful to babies. Families with babies should pay attention to choosing mosquito repellent products
Pytorch exercise items
The difference between CONDA and pip
(翻译)异步编程:Async/Await在ASP.NET中的介绍
Learning notes -- principles and comparison of k-d tree and IKD tree
Machine learning | simple but feature standardization methods that can improve the effect of the model (comparison and analysis of robustscaler, minmaxscaler, standardscaler)