问题描述
- 请问在java中做导出的时候应该怎么实现下拉列表框
-
这里我给出了我的java源码 望各位大神仔细看看 小弟在此谢谢了
下面这段代码实现的效果是这样
而我想实现的效果是这样的 如图
在计价方式那 有3个选项 可以进行下拉选择的```ruby #这里可以指定高亮语言类型#
package com.devsun.action.pm.room;import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;
import org.springframework.beans.factory.annotation.Autowired;import com.devsun.action.BaseAction;
import com.devsun.biz.pm.BuildingBiz;
import com.devsun.biz.pm.HouseTypeBiz;
import com.devsun.biz.pm.ProjectBiz;
import com.devsun.biz.pm.RoomInfoBiz;
import com.devsun.common.Constants;
import com.devsun.entity.pm.RoomInfo;
import com.dongdong.plugins.permissions.PermissionsAnnotation;/**
- 导出房间面积/价格设置
- @author zhangyan
*
*/
public class ZhangYanAction extends BaseAction {private static final long serialVersionUID = -940973448889670031L;
@Autowired
private RoomInfoBiz roomInfoBiz; //这个是自己获得list的类
@Autowired
private ProjectBiz projectBiz;//这个是自己获得list的类@Autowired
private BuildingBiz BuildingBiz;//这个是自己获得list的类@Autowired
private HouseTypeBiz houseTypeBiz;//这个是自己获得list的类// 楼栋Id
private int buildingid;//导出排序
private int paishu = 0 ;@PermissionsAnnotation(name="导出房间面积/价格设置")
public String execute() throws Exception {
List listRoom = roomInfoBiz.daochu(buildingid,paishu);// 创建excel
int i = 0; int j = 0;
for(RoomInfo info:listRoom){
i = info.getProjectId();
j = info.getBuildingId();
break;
}
String pro = "项目:"+ projectBiz.findById(i).getProjectName()+"--楼栋:"+BuildingBiz.findById(j).getBuildingName();;
String name = pro+"--房间面积价格设置";//以上都是获得导出excel名字 downLoadFile(createExcel(listRoom, name), "房间面积/价格设置.xls"); return null;
}
/**
- 创建excel标题
- @param academyList
- @param title
- @param resultMap
- @return
- @throws Exception
*/
public HSSFWorkbook createExcel(List listRoom, String title)throws Exception {
try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
wb.setSheetName(0, title);
HSSFRow titleRow = sheet.createRow(0);
HSSFRow head = sheet.createRow(1);
// 创建标题
createTitle(wb, titleRow, title);
createCell(wb, head, 0, "编号",HSSFColor.GREY_25_PERCENT.index);
createCell(wb, head, 1, "单元",HSSFColor.GREY_25_PERCENT.index);
createCell(wb, head, 2, "楼层",HSSFColor.GREY_25_PERCENT.index);
createCell(wb, head, 3, "号码",HSSFColor.GREY_25_PERCENT.index);
createCell(wb, head, 4, "房间号码",HSSFColor.GREY_25_PERCENT.index);
createCell(wb, head, 5, "户型构成",HSSFColor.GREY_25_PERCENT.index);
createCell(wb, head, 6, "预售套内面积(㎡)",HSSFColor.RED.index);
createCell(wb, head, 7, "预售建筑面积(㎡)",HSSFColor.RED.index);
createCell(wb, head, 8, "计价方式",HSSFColor.RED.index);
createCell(wb, head, 9, "底单价(元)",HSSFColor.RED.index);
createCell(wb, head, 10, "底总价(元)",HSSFColor.RED.index);
createCell(wb, head, 11,"表单价(元)",HSSFColor.RED.index);
createCell(wb, head, 12, "表总价(元)",HSSFColor.RED.index);
createCell(wb, head, 13, "门牌号",HSSFColor.RED.index);
createCell(wb, head, 14, "销售状态",HSSFColor.GREY_25_PERCENT.index);
sheet.addMergedRegion(getCellRangeAddress(1, 1, 1, 15));
int row=2;//行的记录数
int index=0;
if(listRoom!=null &&listRoom.size()>0){
for (RoomInfo roomInfo : listRoom){
HSSFRow bodyRow = sheet.createRow(row++);
index++;
String jijia = "";
String sellStatus = "";
String menpaihao = ""; //这3个值我把他精简了 获取比较复杂createCell(wb, bodyRow, 0, String.valueOf(roomInfo.getId()), HSSFColor.WHITE.index);//编号 createCell(wb, bodyRow, 1, roomInfo.getUtil()+"单元",HSSFColor.WHITE.index);//单元 createCell(wb, bodyRow, 2, String.valueOf(roomInfo.getStorey()),HSSFColor.WHITE.index);//楼层 createCell(wb, bodyRow, 3, roomInfo.getNum(),HSSFColor.WHITE.index);//号码 createCell(wb, bodyRow, 4, roomInfo.getRoomNum(),HSSFColor.WHITE.index);//房间号码 createCell(wb, bodyRow, 5, houseTypeBiz.findHouseType(roomInfo.getHoseTypeId()),HSSFColor.WHITE.index);//户型 createCell(wb, bodyRow, 6, String.valueOf(roomInfo.getPlanSaleIndoorArea()),HSSFColor.WHITE.index);//预售套内 createCell(wb, bodyRow, 7, String.valueOf(roomInfo.getPlanSaleBuildingArea()),HSSFColor.WHITE.index);//预售建筑 createCell(wb, bodyRow, 8, jijia,HSSFColor.WHITE.index);//计价方式 createCell(wb, bodyRow, 9, String.valueOf(roomInfo.getBottomPrice()),HSSFColor.WHITE.index);//表单价 createCell(wb, bodyRow, 10, String.valueOf(roomInfo.getBottomTotal()),HSSFColor.WHITE.index);//表总价 createCell(wb, bodyRow, 11, String.valueOf(roomInfo.getPrice()),HSSFColor.WHITE.index);//底单价 createCell(wb, bodyRow, 12, String.valueOf(roomInfo.getTotalPrice()),HSSFColor.WHITE.index);//底总价 createCell(wb, bodyRow, 13, menpaihao,HSSFColor.WHITE.index);//门牌号 createCell(wb, bodyRow, 14, sellStatus,HSSFColor.WHITE.index);//销售状态 } // 设置列宽 sheet.setColumnWidth(0, 3000);// 单位 sheet.setColumnWidth(1, 3000);// 单位 sheet.setColumnWidth(2, 3000);// 单位 sheet.setColumnWidth(3, 3000);// 单位 sheet.setColumnWidth(4, 3000);// 单位 sheet.setColumnWidth(5, 3000);// 单位 sheet.setColumnWidth(6, 5000);// 单位 sheet.setColumnWidth(7, 5000);// 单位 sheet.setColumnWidth(8, 3000);// 单位 sheet.setColumnWidth(9, 4000);// 单位 sheet.setColumnWidth(10, 4000);// 单位 sheet.setColumnWidth(11, 4000);// 单位 sheet.setColumnWidth(12, 4000);// 单位 sheet.setColumnWidth(13, 3000);// 单位 sheet.setColumnWidth(14, 3000);// 单位 } return wb;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
- @功能:创建标题
- @param wb
- @param titleRow
- @param title
*/
private void createTitle(HSSFWorkbook wb, HSSFRow titleRow, String title) {
titleRow.setHeight((short) 600);
HSSFCell cell = titleRow.createCell(0);
HSSFRichTextString h = new HSSFRichTextString(title);
// 字体样式
HSSFFont font = wb.createFont();
font.setColor(HSSFColor.BLACK.index);
font.setFontHeightInPoints((short) 18);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
h.applyFont(font);
cell.setCellValue(h);
cell.setCellStyle(getCellDefaultStyle(wb,HSSFColor.WHITE.index));
}
/**
- @功能:获取单元格默认样式
- @param wb
- @return
*/
private HSSFCellStyle getCellDefaultStyle(HSSFWorkbook wb,short color) {
// 设置单元格样式
HSSFCellStyle cellstyle = wb.createCellStyle();
cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION);// 设置水平对齐方式
cellstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 设置垂直对齐方式
cellstyle.setFillForegroundColor(color);
cellstyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellstyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellstyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellstyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellstyle.setWrapText(true);
return cellstyle;
}
/**
- @功能:创建cell
- @param wb
- @param row
- @param col
- @param val
*/
private void createCell(HSSFWorkbook wb, HSSFRow row, int col, String val,short color) {
HSSFCell cell = row.createCell(col);
cell.setCellValue(val);
if(val!=null){
System.out.println(val);
if(!val.endsWith(".00")&&!val.endsWith(".000")){
cell.setCellStyle(getCellDefaultStyle(wb,color));
}else{
cell.setCellStyle(getCellMoneyStyle(wb,color));
}
}else{
cell.setCellStyle(getCellDefaultStyle(wb,color));
}
}
private HSSFCellStyle getCellMoneyStyle(HSSFWorkbook wb,short color) {
// 设置单元格样式
HSSFCellStyle cellstyle = wb.createCellStyle();
cellstyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);// 设置水平对齐方式
cellstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 设置垂直对齐方式
cellstyle.setFillForegroundColor(color);
cellstyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellstyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellstyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellstyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellstyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
cellstyle.setWrapText(true);
return cellstyle;
}@SuppressWarnings("deprecation")
private CellRangeAddress getCellRangeAddress(int startRow, int endRow,
int startColumn, int endColumn) {
return new CellRangeAddress(startRow - 1, endRow - 1, startColumn - 1,
endColumn - 1);
}public int getBuildingid() {
return buildingid;
}public void setBuildingid(int buildingid) {
this.buildingid = buildingid;
}public int getPaishu() {
return paishu;
}public void setPaishu(int paishu) {
this.paishu = paishu;
}
}