POI操作Excel,建立下拉框

來源:互聯網
上載者:User
/** *  */package test.excel;/** * @author seven *2013-4-11上午09:15:23 */import java.io.FileOutputStream;import java.util.Date;import org.apache.poi.hssf.usermodel.DVConstraint;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFDataValidation;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.CellRangeAddressList;public class POI{public static void main(String[] args){String[] list = { "東軟", "華信", "SAP", "海輝" };new POI().createListBox(list);return;}public void createListBox(String[] list){// 檔案初始化HSSFWorkbook wb = new HSSFWorkbook();HSSFSheet sheet = wb.createSheet("new sheet");// 在第一行第一個儲存格,插入下拉框HSSFRow row = sheet.createRow(0);HSSFCell cell = row.createCell(0);// 普通寫入操作cell.setCellValue("請選擇");// 這是實驗// 產生下拉式清單// 只對(0,0)儲存格有效CellRangeAddressList regions = new CellRangeAddressList(0, 0, 0, 0);// 產生下拉框內容DVConstraint constraint = DVConstraint.createExplicitListConstraint(list);// 綁定下拉框和作用地區HSSFDataValidation data_validation = new HSSFDataValidation(regions,constraint);// 對sheet頁生效sheet.addValidationData(data_validation);// 寫入檔案FileOutputStream fileOut;try {fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}// 結束System.out.println("Over");}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.