「javapoi表格」java poi读取excel表格

博主:adminadmin 2022-12-14 13:45:08 65

今天给各位分享javapoi表格的知识,其中也会对java poi读取excel表格进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java poi合并单元表格(求帮助啊)

  /**

  * 

  * @param context

  * @param dictionary

  * @param rows 数据行

  * @param fileName 文件名

  * @param fields 列名

  * @param fieldsName 字段名

  */

 private void outExcelFile(HttpContext context,

   IContextDictionary dictionary, DataRowCollections rows,

   String fileName, ListString fields, ListString fieldsName) {

  int cellSize = fields.size();

  if(cellSize == 0){

   LogManager.debug("没有指定列头信息,无法导出Excel文件!");

   return;

  }

  //============创建样式    start

  HSSFWorkbook workbook = new HSSFWorkbook();

  HSSFSheet sheet = workbook.createSheet();

  //列头字体样式

        HSSFFont headerFont = workbook.createFont();

        headerFont.setFontName("宋体");

        headerFont.setFontHeightInPoints((short) 12);

        headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

        //列头样式

        HSSFCellStyle headerStyle = workbook.createCellStyle();

        headerStyle.setFont(headerFont);

        headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中

        headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中

        headerStyle.setLocked(true);

        headerStyle.setWrapText(true);

        //标题样式

        HSSFFont titleFont = workbook.createFont();

        titleFont.setFontName("宋体");

        titleFont.setFontHeightInPoints((short) 15);

        titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

        

        HSSFCellStyle titleStyle = workbook.createCellStyle();

        titleStyle.setFont(titleFont);

        titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        titleStyle.setLocked(true);

        titleStyle.setWrapText(true);

        

        //普通单元格字体样式

        HSSFFont cellFont = workbook.createFont();

        cellFont.setFontName("宋体");

        cellFont.setFontHeightInPoints((short)12);

        //普通单元格样式

        HSSFCellStyle cellStyle = workbook.createCellStyle();

        cellStyle.setFont(cellFont);

        cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左右居中

        cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中

        cellStyle.setLocked(true);

        cellStyle.setWrapText(true);

        //============创建样式    end

        

        //设置序号列、列宽和标题行     start

        HSSFRow titleRow = sheet.createRow(0);

        titleRow.setHeightInPoints(50);

        HSSFCell titleCell = titleRow.createCell(0);

        titleCell.setCellValue(new HSSFRichTextString(fileName));

        titleCell.setCellStyle(titleStyle);

        //sheet.addMergedRegion(new Region(0,(short)0,0,(short)cellSize));//合并单元格--方法过时                                                                                                                                                                                                                                                                          

        //CellRangeAddress  起始行 结束行 起始列 结束列

        sheet.addMergedRegion(new CellRangeAddress(0,0,(short)0,(short)cellSize));//合并单元格

       

        //显示总的数据个数 start

        HSSFRow countRow = sheet.createRow(1);

        countRow.setHeightInPoints(40);

        HSSFCell countCell = countRow.createCell(0);

        countCell.setCellValue(new HSSFRichTextString("共计专项检查("+rows.size()+")项"));

        countCell.setCellStyle(headerStyle);

        sheet.addMergedRegion(new CellRangeAddress(1,1,(short)0,(short)cellSize));//合并单元格

  //显示总的数据个数 end 

        

        HSSFRow headerRow = sheet.createRow(2);

        headerRow.setHeightInPoints(35);

        HSSFCell headerCell = null;

        

        //序号

        int startIndex = 0 ;

     headerCell = headerRow.createCell(0);

  sheet.setColumnWidth(0, 2000);

  headerCell.setCellValue(new HSSFRichTextString("序号"));

  

  headerCell.setCellStyle(headerStyle);

  startIndex ++ ;

  

  //列头

  for(int i = 0; i  cellSize; i ++){

   sheet.setColumnWidth(startIndex + i, 7000);

   headerCell = headerRow.createCell(startIndex + i);

   headerCell.setCellValue(new HSSFRichTextString(fields.get(i)));

   headerCell.setCellStyle(headerStyle);

  }

  //设置序号列、列宽和标题行   end

  

  //文件正文 start

  int rowNum = 1;

  int rowIndex = 0;

  HSSFRow row = null;

  ListInteger[] cellRangeLst = new ArrayListInteger[](0);

  Integer [] arr = null;

  int l = 0;

  String orgName = ""; 

  for(int j = 2; jrows.size()+2; j++){//循环行

   DataRow dataRow = rows.get(rowIndex); //对应数据库字段

   HSSFCell cell = null;

   row = sheet.createRow(j + 1);

         row.setHeightInPoints(55);

   //序号

   cell = row.createCell(0);

   cell.setCellValue(rowNum++);

      cell.setCellStyle(cellStyle);

      

      if(StringHelper.isNullOrEmpty(orgName)){

       arr = new Integer[2];

       arr[0] = j + 1;

       l =j + 1;

       orgName = dataRow.getString("ORGNAME");

      }else{

       if(!orgName.equals(dataRow.getString("ORGNAME"))){

        arr[1] = j;

        cellRangeLst.add(arr);

        sheet.addMergedRegion(new CellRangeAddress(l,j,1,1));

        arr = new Integer[2];

        l = j+1;

        orgName = dataRow.getString("ORGNAME");

       }

       

       if(rowIndex == rows.size() - 1){

        arr[1] = j+1;

        cellRangeLst.add(arr);

        sheet.addMergedRegion(new CellRangeAddress(l,j+1,1,1));

       }

      }

      

      for(int k = 0; k  cellSize; k++){

       cell = row.createCell(k + startIndex);

       String column = fieldsName.get(k); //对应数据库字段

       String value = "";

       if("APSJ".equals(column)){

        value = getAPSJValue(dataRow.getString(column));

       }else{

        value = dataRow.getString(column);

       }

       cell.setCellValue(new HSSFRichTextString(value));

          cell.setCellStyle(cellStyle);

      }

      rowIndex ++;

  }

  //文件正文 end

  

  //for(Integer[] te : cellRangeLst){

  // sheet.addMergedRegion(new CellRangeAddress(te[0],te[1],1,1));//合并处室单元格

  //}

  

  //下载

  HttpServletResponse response = context.getResponse();

  

  response.setContentType("application/x-download;charset=UTF-8");

  String title = "export";

  try {

   title = java.net.URLEncoder.encode(fileName, "UTF-8");

  } catch (UnsupportedEncodingException e) {

   e.printStackTrace();

  }

  response.addHeader("Content-Disposition", "attachment;filename=" + title + ".xls");

  try {

   OutputStream out = response.getOutputStream();

   workbook.write(out);

   out.flush();

   out.close();

  } catch (IOException e) {

   e.printStackTrace();

  }

 }

//参考一下吧

java poi导出excel要双击才显示换行?

在开始选项卡下面有个玩意叫自动换行,点一下就好了。

如果找不到,全选表格,右击,设置单元格格式,对齐,勾选自动换行即可。

java poi导出excel

用spire.xls.jar也可以导出excel, 代码更简单

import com.spire.xls.ExcelVersion;

import com.spire.xls.Workbook;

import com.spire.xls.Worksheet;

public class InsertArray {

  public static void main(String[] args) {

      //创建Workbook对象

      Workbook wb = new Workbook();

      //获取第一张工作表

      Worksheet sheet = wb.getWorksheets().get(0);

      //定义一维数据

      String[] oneDimensionalArray = new String[]{"苹果", "梨子", "葡萄", "香蕉"};

      //将数组从指定单个格开始写入工作表,true表示纵向写入,设置为false为横向写入

      sheet.insertArray(oneDimensionalArray, 1, 1, true);

      //定义二维数组

      String[][] twoDimensionalArray = new String[][]{

              {"姓名", "年龄", "性别", "学历"},

              {"小张", "25", "男", "本科"},

              {"小王", "24", "男", "本科"},

              {"小李", "26", "女", "本科"}

      };

      //从指定单元格开始写入二维数组到工作表

      sheet.insertArray(twoDimensionalArray, 1, 3);

      //保存文档

      wb.saveToFile("InsertArrays.xlsx", ExcelVersion.Version2016);

  }

}

java操作poi的excel表格的线条怎么变为黑色?

你跑下下面的代码试下吧:\x0d\x0apublic TestExcel(){\x0d\x0a createExcelFile();\x0d\x0a }\x0d\x0a \x0d\x0a private void createExcelFile() {\x0d\x0a HSSFWorkbook hwb = new HSSFWorkbook();\x0d\x0a HSSFSheet sheet = hwb.createSheet("test sheet");\x0d\x0a //创建一个样式\x0d\x0a HSSFCellStyle style = hwb.createCellStyle();\x0d\x0a //设置边框样式\x0d\x0a style.setBorderTop(HSSFCellStyle.BORDER_THIN);\x0d\x0a style.setBorderBottom(HSSFCellStyle.BORDER_THIN);\x0d\x0a style.setBorderLeft(HSSFCellStyle.BORDER_THIN);\x0d\x0a style.setBorderRight(HSSFCellStyle.BORDER_THIN);\x0d\x0a //设置边框颜色\x0d\x0a style.setTopBorderColor(HSSFColor.BLACK.index);\x0d\x0a style.setBottomBorderColor(HSSFColor.BLACK.index);\x0d\x0a style.setLeftBorderColor(HSSFColor.BLACK.index);\x0d\x0a style.setRightBorderColor(HSSFColor.BLACK.index);\x0d\x0a \x0d\x0a for(int j=0;j

回答于 2022-11-16

Java 利用poi 导出excel表格如何在导出时自由选择路径?

导出时自由选择路径的代码如下:

1、后台输出Excel文件代码:

OutputStream output = response.getOutputStream();

response.reset();

response.setHeader("Content-disposition", "attachment; filename=" + path);

response.setContentType("Content-Type:application/vnd.ms-excel ");

wb.write(output);

output.close();

2、前端代码:

window.open("getExcelList","_blank");

导出excel数据

* @param id

* @param m

* @return

*/

@RequestMapping("/exportExcel")

public void exportExcel(@RequestParam("id") Integer id, Model m,HttpServletRequest req, HttpServletResponse resp) {

try {

ExportExcelP2pLoanPlanVo ex = new ExportExcelP2pLoanPlanVo();

String[] headers = {"最迟还款日", "还款金额","剩余几天","逾期几天", "罚息","是否垫付","状态","是否发放收益"};

ListP2pLoanPlanVo dataset = new ArrayListP2pLoanPlanVo();

ListP2pLoanPlan plans = this.planService.getListByLoan(id);

for (int i = 0; i plans.size(); i++) {

P2pLoanPlanVo p2pLoanPlanVo = new P2pLoanPlanVo();

p2pLoanPlanVo.setRepayDate(plans.get(i).getRepayDate());

p2pLoanPlanVo.setRepayAmount(plans.get(i).getRepayAmount());

if(plans.get(i).getRepayDays() = 0 plans.get(i).getStatus() == 0){

p2pLoanPlanVo.setRepayDays(plans.get(i).getRepayDays());

}else{

p2pLoanPlanVo.setRepayDays(0);

}

if(plans.get(i).getRepayDays() 0 plans.get(i).getStatus() == 0){

p2pLoanPlanVo.setRepayYqDays(-plans.get(i).getRepayDays());

}else{

p2pLoanPlanVo.setRepayYqDays(0);

}

javapoi表格的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java poi读取excel表格、javapoi表格的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-14,除非注明,否则均为首码项目网原创文章,转载请注明出处。