激情综合色五月六月婷婷,久久久久久综合网天天,国产精品久久久久精品综合紧无码

  • 2022-10-28
宇哥博客 后端開發 Java使用POI生成Excel文件,附jar包下載

Java使用POI生成Excel文件,附jar包下載

本文所用的是 POI3.16 jar包,生成 Excel2007 格式的文件。代碼中包含常用的字體大小、字體顏色、背景色、寬度、行高、內容自動換行等設置,以下為完整樣例代碼:

package com.test;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;

public class ExportExcel {

	public static SXSSFWorkbook export(List<Map<String, Object>> list){
		  SXSSFWorkbook  workBook = null;
		  List<String> cellTitle=new ArrayList<String>();//設置表頭
		  cellTitle.add("No");
		  cellTitle.add("申請日期");
		  cellTitle.add("是否通過");
		  cellTitle.add("備注");
		  try {
			   workBook = new SXSSFWorkbook(10000);//創建工作薄
			   SXSSFSheet   sheet = (SXSSFSheet) workBook.createSheet("Sheet1");//Sheet名稱
			 
			   XSSFFont font = (XSSFFont) workBook.createFont();
			   font.setColor(XSSFFont.COLOR_NORMAL);
			   font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);//設置字體加粗
			   font.setColor((short) 2);//設置字體為紅色
			   font.setFontHeightInPoints((short) 18);//設置字體大小
			   XSSFCellStyle cellStyle = (XSSFCellStyle) workBook.createCellStyle();//創建格式
			   cellStyle.setFont(font);
			   cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
			   cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
			   cellStyle.setFillForegroundColor((short) 13);//設置背景色(13為黃色)
			   cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);//設置填充
			   
			   SXSSFRow titleRow = (SXSSFRow) sheet.createRow((short) 0);//第一行標題
			   //設置寬度
			   sheet.setColumnWidth(0, 256 * 8);
			   sheet.setColumnWidth(1, 256 * 15);
			   sheet.setColumnWidth(2, 256 * 8);
			   sheet.setColumnWidth(3, 256 * 15);
			   
			   titleRow.setHeightInPoints(100);//設置行高
			   
				for (int i = 0, size = cellTitle.size(); i < size; i++) {
					SXSSFCell cell = (SXSSFCell) titleRow.createCell(i, 0);
					cell.setCellStyle(cellStyle);
					cell.setCellType(XSSFCell.CELL_TYPE_STRING);
					cell.setCellValue(cellTitle.get(i));
				}
				//從第二行開始寫數據
				if (list != null && !list.isEmpty()) {
					for (int i = 0, size = list.size(); i < size; i++) {
						SXSSFRow row = (SXSSFRow) sheet.createRow(i + 1);
						for (int j = 0, length = cellTitle.size(); j < length; j++) {
							XSSFCellStyle style = (XSSFCellStyle) workBook.createCellStyle();//創建格式
							SXSSFCell cell = (SXSSFCell) row.createCell(j, 0);//
							cell.setCellType(XSSFCell.CELL_TYPE_STRING);//
							style.setVerticalAlignment(VerticalAlignment.CENTER);//單元格文字垂直居中
							switch (j) {
								case 0:
									cell.setCellValue( Float.parseFloat(list.get(i).get("no").toString()) );//設置成數字格式
									style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
									cell.setCellStyle(style);
									break;
								case 1:
									cell.setCellValue( list.get(i).get("date").toString() );
									style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
									cell.setCellStyle(style);
									break;
								case 2:
									cell.setCellValue( list.get(i).get("is_pass").toString() );
									style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
									cell.setCellStyle(style);
									break;
								case 3:
									cell.setCellValue( list.get(i).get("remark").toString() );
									style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
									style.setWrapText(true);//設置自動換行
									cell.setCellStyle(style);
									break;
								default:
									cell.setCellValue("");
							}
						}
					}
				}
				
			String exportFileName = "e://"+new Date().getTime() + ".xlsx";
			   File file = new File( exportFileName);
			   FileOutputStream outStream = new FileOutputStream(file);
			   workBook.write(outStream);
			   outStream.flush();
			   outStream.close();
			   System.out.println("導出文件路徑:"+file.getPath());
				
		  }catch(Exception e){
		   System.out.println("異常!"+e.getMessage());
		   e.printStackTrace();
		   return null;
		  }
		return workBook;
		  
	}
	
	public static void main(String[] args) {
		
		List<Map<String, Object>> list=new ArrayList();

		Map map1=new HashMap();
		map1.put("no", "1");
		map1.put("date", "2022-09-01");
		map1.put("is_pass", "是");
		map1.put("remark", "備注備注備注備注備注備注備注備注備注備注備注1111");
		list.add(map1);
		
		Map map2=new HashMap();
		map2.put("no", "2");
		map2.put("date", "2022-09-03");
		map2.put("is_pass", "是");
		map2.put("remark", "備注備注備注備注備注備注備注備注備注備注備注22222");
		list.add(map2);
		
		Map map3=new HashMap();
		map3.put("no", "3");
		map3.put("date", "2022-09-11");
		map3.put("is_pass", "否");
		map3.put("remark", "備注備注備注備注備注備注備注備注33333333333333333");
		list.add(map3);
		
		export(list);
	}
}

生成 Excel 文件效果:

圖 | Excel文件

更多

相關文章:

Java POI寫Excel控制列寬 http://www.callingbugs.com/1100.html

POI相關jar包下載:https://wwz.lanzout.com/b031hfrmb (密碼:8njz)

包含文件:
xmlbeans-3.1.0.jar
poi-ooxml-schemas-3.16-beta2.jar
poi-ooxml-3.16-beta2.jar
poi-3.16-beta2.jar
commons-collections4-4.1.jar

其中設置字體顏色、背景色對應色值可參考下圖:

font.setColor((short) 2);//設置字體為紅色
cellStyle.setFillForegroundColor((short) 13);//設置背景色(13為黃色)
圖2 | 顏色對照表

“圖2”轉自:https://blog.csdn.net/w779050550/article/details/81094221, 包含有生成的代碼;也可進入 https://blog.csdn.net/Han_Yi_To/article/details/119644992 查看。

本文來自網絡,不代表本站立場,轉載請注明出處。http://www.callingbugs.com/3581.html

發表評論

您的電子郵箱地址不會被公開。

返回頂部
激情综合色五月六月婷婷,久久久久久综合网天天,国产精品久久久久精品综合紧无码