当前位置:网站首页>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 :
边栏推荐
- 爬虫代码基础教学
- Journal quotidien des questions (11)
- [classes and objects] explain classes and objects in simple terms
- Condition annotation in uni-app realizes cross segment compatibility, navigation jump and parameter transfer, component creation and use, and life cycle function
- 每日刷题记录 (十一)
- Operation principle of lua on C: Foundation
- Code management tools
- 【5G NR】UE注册流程
- Reading notes of "learn to ask questions"
- 【code】偶尔取值、判空、查表、验证等
猜你喜欢
SQL implementation merges multiple rows of records into one row
On the practice of performance optimization and stability guarantee
Sorting out the core ideas of the pyramid principle
Summary of the design and implementation of the weapon system similar to the paladin of vitality
The pressure of large institutions in the bear market has doubled. Will the giant whales such as gray scale, tether and micro strategy become 'giant thunder'?
Yolov2 learning and summary
Selenium - by changing the window size, the width, height and length of different models will be different
10000小时定律不会让你成为编程大师,但至少是个好的起点
2022-06-23 VGMP-OSPF-域間安全策略-NAT策略(更新中)
2022-06-23 vgmp OSPF inter domain security policy NAT policy (under update)
随机推荐
Journal quotidien des questions (11)
[Code] if (list! = null & list. Size() > 0) optimization, set empty judgment implementation method
Daily question brushing record (11)
These two mosquito repellent ingredients are harmful to babies. Families with babies should pay attention to choosing mosquito repellent products
爬虫代码基础教学
MATLAB如何修改默认设置
Golang operation redis: write and read kV data
How does the insurance company check hypertension?
利用C#实现Pdf转图片
100000 bonus is divided up. Come and meet the "sister who braves the wind and waves" among the winners
My 2020 summary "don't love the past, indulge in moving forward"
卡特兰数(Catalan)的应用场景
instanceof
crontab定时任务
[5g NR] UE registration process
The essence of interview
Understand software testing
Summary of UI module design and practical application of agent mode
mongodb
What are the characteristics and functions of the scientific thinking mode of mechanical view and system view