91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

在Java Web中Excel文件如何使用POI實現導出

發布時間:2020-11-21 15:59:29 來源:億速云 閱讀:203 作者:Leah 欄目:編程語言

在Java Web中Excel文件如何使用POI實現導出?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

采用Spring mvc架構:

Controller層代碼如下

@Controller
public class StudentExportController{
  @Autowired
  private StudentExportService studentExportService;
  @RequestMapping(value = "/excel/export")
  public void exportExcel(HttpServletRequest request, HttpServletResponse response)
  throws Exception {
    List<Student> list = new ArrayList<Student>();
    list.add(new Student(1000,"zhangsan","20"));
    list.add(new Student(1001,"lisi","23"));
    list.add(new Student(1002,"wangwu","25"));
    HSSFWorkbook wb = studentExportService.export(list);
    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-disposition", "attachment;filename=student.xls");
    OutputStream ouputStream = response.getOutputStream();
    wb.write(ouputStream);
    ouputStream.flush();
    ouputStream.close();
  }
}

Service層代碼如下:

@Service
public class StudentExportService {
  String[] excelHeader = { "Sno", "Name", "Age"};
  public HSSFWorkbook export(List<Campaign> list) {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("Campaign");
    HSSFRow row = sheet.createRow((int) 0);
    HSSFCellStyle style = wb.createCellStyle();
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    for (int i = 0; i < excelHeader.length; i++) {
      HSSFCell cell = row.createCell(i);
      cell.setCellValue(excelHeader[i]);
      cell.setCellStyle(style);
      sheet.autoSizeColumn(i);
    }
    for (int i = 0; i < list.size(); i++) {
      row = sheet.createRow(i + 1);
      Student student = list.get(i);
      row.createCell(0).setCellValue(student.getSno());
      row.createCell(1).setCellValue(student.getName());
      row.createCell(2).setCellValue(student.getAge());
    }
    return wb;
  }
}

前臺的js代碼如下:

<script>
function exportExcel(){
   location.href="excel/export" rel="external nofollow" ;
   <!--這里不能用ajax請求,ajax請求無法彈出下載保存對話框-->
 }
</script>

設置Excel樣式以及注意點:

String[] excelHeader = { "所屬區域(地市)", "機房", "機架資源情況", "", "", "", "",
  "", "端口資源情況", "", "", "", "", "", "機位資源情況", "", "", "設備資源情況",
  "", "", "IP資源情況", "", "", "", "", "網絡設備數" };
String[] excelHeader1 = { "", "", "總量(個)", "空閑(個)", "預占(個)", "實占(個)",
  "自用(個)", "其它(個)", "總量(個) ", "在用(個)", "空閑(個)", "總帶寬(M)",
  "在用帶寬(M)", "空閑帶寬(M)", "總量(個)", "在用(個)", "空閑(個)", "設備總量(個)",
  "客戶設備(個)", "電信設備(個)", "總量(個)", "空閑(個)", "預占用(個)", "實占用(個)",
  "自用(個)", "" };
// 單元格列寬
int[] excelHeaderWidth = { 150, 120, 100, 100, 100, 100, 100, 100, 100,
  100, 100, 120, 120, 120, 120, 120, 120, 150, 150, 150, 120,
  120, 150, 150, 120, 150 };
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("機房報表統計");
HSSFRow row = sheet.createRow((int) 0);
HSSFCellStyle style = wb.createCellStyle();
// 設置居中樣式
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直居中
// 設置合計樣式
HSSFCellStyle style1 = wb.createCellStyle();
Font font = wb.createFont();
font.setColor(HSSFColor.RED.index);
font.setBoldweight(Font.BOLDWEIGHT_BOLD); // 粗體
style1.setFont(font);
style1.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中
style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直居中
// 合并單元格
// first row (0-based) last row (0-based) first column (0-based) last
// column (0-based)
sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));
sheet.addMergedRegion(new CellRangeAddress(0, 1, 1, 1));
sheet.addMergedRegion(new CellRangeAddress(0, 0, 2, 7));
sheet.addMergedRegion(new CellRangeAddress(0, 0, 8, 13));
sheet.addMergedRegion(new CellRangeAddress(0, 0, 14, 16));
sheet.addMergedRegion(new CellRangeAddress(0, 0, 17, 19));
sheet.addMergedRegion(new CellRangeAddress(0, 0, 20, 24));
sheet.addMergedRegion(new CellRangeAddress(0, 1, 25, 25));
// 設置列寬度(像素)
for (int i = 0; i < excelHeaderWidth.length; i++) {
  sheet.setColumnWidth(i, 32 * excelHeaderWidth[i]);
}
// 添加表格頭
for (int i = 0; i < excelHeader.length; i++) {
  HSSFCell cell = row.createCell(i);
  cell.setCellValue(excelHeader[i]);
  cell.setCellStyle(style);
}
row = sheet.createRow((int) 1);
for (int i = 0; i < excelHeader1.length; i++) {
  HSSFCell cell = row.createCell(i);
  cell.setCellValue(excelHeader1[i]);
  cell.setCellStyle(style);
}

注意點1:合并單元格    new CellRangeAddress(int,int,int,int)
first row (0-based) ,last row (0-based), first column (0-based),last column (0-based)

注意點2:合并單元格
String[] excelHeader = { "所屬區域(地市)", "機房", "機架資源情況", "", "", "", "","", "端口資源情況", "", "", "", "", "", "機位資源情況", "", "", "設備資源情況","", "", "IP資源情況", "", "", "", "", "網絡設備數" };

合并以后的單元格雖然是一個,但是仍然要保留其單元格內容,此處用空字符串代替,否則后續表頭顯示不出

注意點3:填充單元格

正確寫法:

HSSFCell cell = row.createCell(i);
cell.setCellValue(excelHeader1[i]);
cell.setCellStyle(style);

錯誤寫法:

row.createCell(i).setCellValue(excelHeader1[i]);
row.createCell(i).setCellStyle(style);

關于在Java Web中Excel文件如何使用POI實現導出問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

北碚区| 阿城市| 公主岭市| 玉龙| 北流市| 法库县| 巴彦县| 容城县| 南靖县| 安庆市| 大兴区| 边坝县| 化州市| 泽州县| 章丘市| 叶城县| 依安县| 洪湖市| 宝鸡市| 兴化市| 平昌县| 三明市| 红河县| 视频| 横峰县| 钟山县| 德格县| 高台县| 英吉沙县| 高邮市| 忻州市| 右玉县| 离岛区| 黄龙县| 重庆市| 绵竹市| 乐安县| 罗城| 竹溪县| 双鸭山市| 隆尧县|