进击的小伙伴 2013-10-23 06:49 采纳率: 0%
浏览 1578

请问在java中做导出的时候应该怎么实现下拉列表框

这里我给出了我的java源码 望各位大神仔细看看 小弟在此谢谢了
下面这段代码实现的效果是这样
CSDN移动问答

而我想实现的效果是这样的 如图
在计价方式那 有3个选项 可以进行下拉选择的
CSDN移动问答

```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;
    }

}




  • 写回答

1条回答 默认 最新

  • 就叫这吧 2023-09-14 17:33
    关注
    
    import org.apache.poi.ss.usermodel.*;  
    import org.apache.poi.ss.util.CellRangeAddressList;  
    import org.apache.poi.xssf.usermodel.*;  
      
    import java.io.FileOutputStream;  
    import java.io.IOException;  
      
    public class ExcelDropdownListExample {  
        public static void main(String[] args) throws IOException {  
            // 创建工作簿和工作表  
            Workbook workbook = new XSSFWorkbook();  
            Sheet sheet = workbook.createSheet("Dropdown List");  
      
            // 创建下拉列表的数据  
            String[] dropdownData = {"Option 1", "Option 2", "Option 3"};  
      
            // 在单元格A1:A10中创建下拉列表框  
            DataValidationHelper dvHelper = new XSSFDataValidationHelper((XSSFWorkbook) workbook);  
            CellRangeAddressList addressList = new CellRangeAddressList(0, 9, 0, 0);  
            XSSFDataValidation validation = (XSSFDataValidation) dvHelper.createExplicitListConstraint(addressList, dropdownData);  
            validation.setShowErrorBox(true);  
            sheet.addValidationData(validation);  
      
            // 将工作簿写入文件  
            FileOutputStream fileOut = new FileOutputStream("dropdown_list_example.xlsx");  
            workbook.write(fileOut);  
            fileOut.close();  
        }  
    }
    

    在上面的示例代码中,我们首先创建一个工作簿和一个工作表。然后,我们创建一个字符串数组,其中包含要在下拉列表框中显示的数据。接下来,我们使用DataValidationHelper类创建一个CellRangeAddressList对象,该对象定义了下拉列表框的范围。然后,我们使用createExplicitListConstraint方法创建一个DataValidation对象,并将下拉列表数据传递给它。最后,我们将DataValidation对象添加到工作表中,并将工作簿写入文件。

    评论

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名