java 生成Excel文件

public String excelDocumentMonthly(List<Map> list,HttpServletRequest request ) throws Exception { 
String[] ColumnT={"序号","年月","数量","各类别数量","收发类别"};
String serverRealPath = this.getServerRealPath(request);  // 工程路径
String outputFile=serverRealPath+Config.getParam("gwjh.templatedownpath")+"dayin.xls";
//String outputFile="D://dayin.xls";    
try{
// 创建新的Excel 工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFCellStyle style = workbook.createCellStyle(); 
HSSFSheet sheet = workbook.createSheet();
HSSFFont font = workbook.createFont();
style.setBorderBottom(HSSFCellStyle.BORDER_THIN );//下边框
       style.setBorderLeft(HSSFCellStyle.BORDER_THIN );//左边框
    style.setBorderRight(HSSFCellStyle.BORDER_THIN );//右边框 
    style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框 
//显示列名
       for(int i=0;i<ColumnT.length;i++){
       HSSFRow rowT = sheet.createRow((short)0);
    HSSFCell cellT = rowT.createCell((short)i );
    sheet.setColumnWidth((short)i,(short)5000);   
    rowT.setHeight((short)500);  
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中     
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中       
cellT.setCellStyle(style);  
    cellT.setCellType(HSSFCell.CELL_TYPE_STRING);
       cellT.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell中文编码;
cellT.setCellValue(ColumnT[i]);
       }

for(int w=1 ; w<=list.size() ; w++){

Map curren = list.get(w-1);
if(w%2!=0){
//显示序号
HSSFRow  row= sheet.createRow((short)w);//在索引0的位置创建单元格(左上端)
  HSSFCell cell = row.createCell((short)0 );
sheet.addMergedRegion(new Region((short)w , (short) 0, (short)(w+1), (short)0));
//设置行高列宽
sheet.setColumnWidth((short)w,(short)5000);   
    row.setHeight((short)800);   
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中  
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中     
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中       
cell.setCellStyle(style);      
// 定义单元格为字符串类型
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell中文编码;
cell.setCellValue(curren.get("INDEX").toString());

//显示年份

row = sheet.createRow((short)w);//在索引0的位置创建单元格(左上端)
cell = row.createCell((short)1 );
sheet.addMergedRegion(new Region((short)w , (short) 1, (short)(w+1), (short)1));
sheet.setColumnWidth((short)w,(short)3000);   
row.setHeight((short)500);
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中  
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中     
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中       
cell.setCellStyle(style);  
// 定义单元格为字符串类型
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
    cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell中文编码;
   // cell.setCellValue(curren.get("TIMEMONTH").toString());
    cell.setCellValue( curren.get("TIMEMONTH").toString().substring(0,4)+'-'+curren.get("TIMEMONTH").toString().substring(4));
    //显示总数
cell = row.createCell((short)2 );
sheet.addMergedRegion(new Region((short)w , (short) 2, (short)(w+1), (short)2));
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中  
sheet.setColumnWidth((short)w,(short)3000);   
row.setHeight((short)500);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中     
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中       
cell.setCellStyle(style);  
// 定义单元格为字符串类型
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell中文编码;
cell.setCellValue(curren.get("RSCOUNT").toString());
cell = row.createCell((short)3 );
   sheet.setColumnWidth((short)w,(short)3000);   
   row.setHeight((short)400);
      style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中     
      style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中       
  cell.setCellStyle(style);  
// 定义单元格为字符串类型
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell中文编码;
cell.setCellValue(curren.get("COUNT").toString());

cell = row.createCell((short)4 );
sheet.setColumnWidth((short)w,(short)3000);   
row.setHeight((short)400);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中     
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中       
cell.setCellStyle(style);  
// 定义单元格为字符串类型
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell中文编码;
cell.setCellValue(curren.get("RSTYPE").toString());
}else{
int t=w-1;
            HSSFRow row = sheet.createRow((short)w);//在索引0的位置创建单元格(左上端)
  HSSFCell cell = row.createCell((short)0 );
  sheet.setColumnWidth((short)w,(short)3000);   
row.setHeight((short)0);
cell.setCellStyle(style);  
cell.setCellValue("");
 row = sheet.createRow((short)w);//在索引0的位置创建单元格(左上端)
   cell = row.createCell((short)1 );
  sheet.setColumnWidth((short)w,(short)3000);   
row.setHeight((short)0);
cell.setCellStyle(style);  
cell.setCellValue("");
row = sheet.createRow((short)w);
cell = row.createCell((short)2 );
sheet.setColumnWidth((short)w,(short)3000);   
row.setHeight((short)0);
cell.setCellStyle(style);  
    cell.setCellValue("");
    
  row= sheet.createRow((short)w);//在索引0的位置创建单元格(左上端)
  cell = row.createCell((short)3 );
   sheet.setColumnWidth((short)w,(short)5000);   
   row.setHeight((short)400);
      style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中     
      style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中       
  cell.setCellStyle(style);  
// 定义单元格为字符串类型
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell中文编码;
cell.setCellValue(curren.get("COUNT").toString());

cell = row.createCell((short)4 );
sheet.setColumnWidth((short)w,(short)5000);   
row.setHeight((short)400);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中     
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中       
cell.setCellStyle(style);  
// 定义单元格为字符串类型
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell中文编码;
cell.setCellValue(curren.get("RSTYPE").toString());
}
}
// 新建一输出文件流
FileOutputStream fOut = new FileOutputStream(outputFile);
// 把相应的Excel 工作簿存盘
workbook.write(fOut);
fOut.flush();
// 操作结束,关闭文件
fOut.close();
System.out.println("文件生成...");
return Config.getParam("gwjh.templatedownpath")+"dayin.xls";
}catch(Exception e) {
System.out.println("已运行 xlCreate() : " + e );
return "0";
}
}
要在 Java生成 Excel 文件,有多种方式可以实现。其中一种比较常见的方式是使用 Apache POI 库。下面是一个简单的示例代码,可以使用 Apache POI 在 Java生成一个 Excel 文件,并将数据写入其中。 ```java import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelGenerator { public static void main(String[] args) throws IOException { // 创建一个新的 Excel 工作簿 XSSFWorkbook workbook = new XSSFWorkbook(); // 创建一个新的工作表 String sheetName = "Sheet1"; workbook.createSheet(sheetName); // 在工作表中创建一行 Row row = workbook.getSheet(sheetName).createRow(0); // 在行中创建单元格,并写入数据 Cell cell1 = row.createCell(0); cell1.setCellValue("Hello"); Cell cell2 = row.createCell(1); cell2.setCellValue("World"); // 保存 Excel 文件 String filename = "example.xlsx"; FileOutputStream outputStream = new FileOutputStream(filename); workbook.write(outputStream); workbook.close(); } } ``` 在这个示例中,我们首先创建了一个新的 Excel 工作簿。然后,我们创建了一个新的工作表,并在其中创建了一行。最后,我们在行中创建了两个单元格,并将数据写入其中。最后,我们将 Excel 文件保存到磁盘上。 需要注意的是,这个示例中使用的是 XSSFWorkbook 类,它可以创建 Excel 2007 格式的文件。如果需要创建 Excel 2003 格式的文件,可以使用 HSSFWorkbook 类。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值