Pom:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.3</version>
</dependency>
写Excel文件:
package com.zkfr.test.utils;
//生成Excel文件
import com.zkfr.test.bo.HourData;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.joda.time.DateTime;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
import java.util.TreeMap;
public class ExcelUtil
{
public static void exportOneHourDataExcelFile(String dir, String fileName, DateTime dataDate, TreeMap<DateTime, HourData> hourDataMap )
{
try
{
//判断文件是否存在
Files.deleteIfExists(Paths.get(dir + fileName) );
//
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet hssfSheet = workbook.createSheet();
hssfSheet.setDefaultRowHeight( (short) 320 );
hssfSheet.setDefaultColumnWidth( (short) 16 );
//表头
HSSFCellStyle hssfCellStyle = workbook.createCellStyle();
hssfCellStyle.setAlignment( HorizontalAlignment.CENTER );
hssfCellStyle.setVerticalAlignment( VerticalAlignment.CENTER );
hssfCellStyle.setWrapText( true );
//创建第一行: 创建行的单元格,从0开始
HSSFRow hssfRow = hssfSheet.createRow( 0 );
HSSFCell hssfCell = hssfRow.createCell(0);
hssfCell.setCellValue("短期预测(T+3至T+7)");
CellRangeAddress region = new CellRangeAddress(0, 0, 0, 6);
hssfSheet.addMergedRegion(region);
hssfCell.setCellStyle( hssfCellStyle );
//创建第2行
HSSFRow hssfRow1 = hssfSheet.createRow(1);
//第0列
HSSFCell cell10 = hssfRow1.createCell(0);
cell10.setCellValue("日期: " + dataDate.toString( "yyyy-MM-dd" ) );
cell10.setCellStyle( hssfCellStyle );
//第1列
HSSFCell cell11 = hssfRow1.createCell(1);
cell11.setCellValue( "预测" );
CellRangeAddress region11 = new CellRangeAddress(1, 1, 1, 3);
hssfSheet.addMergedRegion( region11 );
cell11.setCellStyle( hssfCellStyle );
//第2列
HSSFCell cell12 = hssfRow1.createCell(4);
cell12.setCellValue( "实际" );
CellRangeAddress region12 = new CellRangeAddress(1, 1, 4, 6);
hssfSheet.addMergedRegion( region12 );
cell12.setCellStyle( hssfCellStyle );
//创建第3行
HSSFRow hssfRow2 = hssfSheet.createRow( 2 );
//第0列:时间
HSSFCell cell20 = hssfRow2.createCell(0);
cell20.setCellValue( "时间" );
cell20.setCellStyle( hssfCellStyle );
//第1列:发电量(kWh)
HSSFCell cell21 = hssfRow2.createCell(1);
cell21.setCellValue( "发电量(kWh)" );
cell21.setCellStyle( hssfCellStyle );
//第2列:平均风速
HSSFCell cell22 = hssfRow2.createCell(2);
cell22.setCellValue( "平均风速" );
cell22.setCellStyle( hssfCellStyle );
//第3列:功率
HSSFCell cell23 = hssfRow2.createCell(3);
cell23.setCellValue( "功率" );
cell23.setCellStyle( hssfCellStyle );
//第4列:发电量(kWh)
HSSFCell cell24 = hssfRow2.createCell(4);
cell24.setCellValue( "发电量(kWh)" );
cell24.setCellStyle( hssfCellStyle );
//第5列:平均风速
HSSFCell cell25 = hssfRow2.createCell(5);
cell25.setCellValue( "平均风速" );
cell25.setCellStyle( hssfCellStyle );
//第6列:功率
HSSFCell cell26 = hssfRow2.createCell(6);
cell26.setCellValue( "功率" );
cell26.setCellStyle( hssfCellStyle );
//创建第4行~第26行
int nRowIndex = 3;
for(Map.Entry<DateTime, HourData> hourDataEntry : hourDataMap.entrySet())
{
HSSFRow hssfRow3 = hssfSheet.createRow( nRowIndex );
//第0列:时间
HSSFCell cell30 = hssfRow3.createCell(0);
cell30.setCellValue( hourDataEntry.getKey().toString( "HH:mm" ) );
cell30.setCellStyle( hssfCellStyle );
//第1列:发电量(kWh)
HSSFCell cell31 = hssfRow3.createCell(1);
cell31.setCellValue( String.format("%.2f", hourDataEntry.getValue().getForecastElectricity()) );
cell31.setCellStyle( hssfCellStyle );
//第2列:平均风速
HSSFCell cell32 = hssfRow3.createCell(2);
cell32.setCellValue( String.format("%.2f", hourDataEntry.getValue().getForecastWindSpeed()) );
cell32.setCellStyle( hssfCellStyle );
//第3列:功率
HSSFCell cell33 = hssfRow3.createCell(3);
cell33.setCellValue(String.format("%.2f", hourDataEntry.getValue().getForecastPower()) );
cell33.setCellStyle( hssfCellStyle );
//第4列:发电量(kWh)
HSSFCell cell34 = hssfRow3.createCell(4);
cell34.setCellValue( String.format("%.2f", hourDataEntry.getValue().getRealElectricity()) );
cell34.setCellStyle( hssfCellStyle );
//第5列:平均风速
HSSFCell cell35 = hssfRow3.createCell(5);
cell35.setCellValue( String.format("%.2f", hourDataEntry.getValue().getRealWindSpeed()) );
cell35.setCellStyle( hssfCellStyle );
//第6列:功率
HSSFCell cell36 = hssfRow3.createCell(6);
cell36.setCellValue( String.format("%.2f", hourDataEntry.getValue().getRealPower()) );
cell36.setCellStyle( hssfCellStyle );
nRowIndex = nRowIndex+1;
}
//写入文件
FileOutputStream fileOutputStream = new FileOutputStream( dir + fileName );
workbook.write( fileOutputStream );
workbook.close();
fileOutputStream.close();
}
catch( Exception e )
{
e.printStackTrace();
}
}
}