javapoiexcel文件|java操作poi怎么更改excel中的数据

javapoiexcel文件|java操作poi怎么更改excel中的数据的第1张示图

Ⅰ java 使用POI导出Excel文件,打开导出文件时报错

在保护状态下execl的格式有可能正在被使用,你这边修改,准确说是线程冲突,一般excel值会作为导出文件的模板,是不会编辑的。你可以在读的时候判断execl是否正在被使用。下面的代码问题,你可以参考package com.hwt.glmf.common; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletResponse; 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; /** * 导出Excel公共方法 * @version 1.0 * * @author wangcp * */ public class ExportExcel extends BaseAction { //显示的导出表的标题 private String title; //导出表的列名 private String[] rowName ; private List<Object[]> dataList = new ArrayList<Object[]>(); HttpServletResponse response; //构造方法,传入要导出的数据 public ExportExcel(String title,String[] rowName,List<Object[]> dataList){ this.dataList = dataList; this.rowName = rowName; this.title = title; } /* * 导出数据 * */ public void export() throws Exception{ try{ HSSFWorkbook workbook = new HSSFWorkbook(); // 创建工作簿对象 HSSFSheet sheet = workbook.createSheet(title); // 创建工作表 // 产生表格标题行 HSSFRow rowm = sheet.createRow(0); HSSFCell cellTiltle = rowm.createCell(0); //sheet样式定义【getColumnTopStyle()/getStyle()均为自定义方法 – 在下面 – 可扩展】 HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);//获取列头样式对象 HSSFCellStyle style = this.getStyle(workbook); //单元格样式对象 sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, (rowName.length-1))); cellTiltle.setCellStyle(columnTopStyle); cellTiltle.setCellValue(title); // 定义所需列数 int columnNum = rowName.length; HSSFRow rowRowName = sheet.createRow(2); // 在索引2的位置创建行(最顶端的行开始的第二行) // 将列头设置到sheet的单元格中 for(int n=0;n<columnNum;n++){ HSSFCell cellRowName = rowRowName.createCell(n); //创建列头对应个数的单元格 cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); //设置列头单元格的数据类型 HSSFRichTextString text = new HSSFRichTextString(rowName[n]); cellRowName.setCellValue(text); //设置列头单元格的值 cellRowName.setCellStyle(columnTopStyle); //设置列头单元格样式 } //将查询出的数据设置到sheet对应的单元格中 for(int i=0;i<dataList.size();i++){ Object[] obj = dataList.get(i);//遍历每个对象 HSSFRow row = sheet.createRow(i+3);//创建所需的行数 for(int j=0; j<obj.length; j++){ HSSFCell cell = null; //设置单元格的数据类型 if(j == 0){ cell = row.createCell(j,HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(i+1); }else{ cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING); if(!"".equals(obj[j]) && obj[j] != null){ cell.setCellValue(obj[j].toString()); //设置单元格的值 } } cell.setCellStyle(style); //设置单元格样式 } } //让列宽随着导出的列长自动适应 for (int colNum = 0; colNum < columnNum; colNum++) { int columnWidth = sheet.getColumnWidth(colNum) / 256; for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) { HSSFRow currentRow; //当前行未被使用过 if (sheet.getRow(rowNum) == null) { currentRow = sheet.createRow(rowNum); } else { currentRow = sheet.getRow(rowNum); } if (currentRow.getCell(colNum) != null) { HSSFCell currentCell = currentRow.getCell(colNum); if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) { int length = currentCell.getStringCellValue().getBytes().length; if (columnWidth < length) { columnWidth = length; } } } } if(colNum == 0){ sheet.setColumnWidth(colNum, (columnWidth-2) * 256); }else{ sheet.setColumnWidth(colNum, (columnWidth+4) * 256); } } if(workbook !=null){ try { String fileName = "Excel-" + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls"; String headStr = "attachment; filename=\"" + fileName + "\""; response = getResponse(); response.setContentType("APPLICATION/OCTET-STREAM"); response.setHeader("Content-Disposition", headStr); OutputStream out = response.getOutputStream(); workbook.write(out); } catch (IOException e) { e.printStackTrace(); } } }catch(Exception e){ e.printStackTrace(); } } /* * 列头单元格样式 */ public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) { // 设置字体 HSSFFont font = workbook.createFont(); //设置字体大小 font.setFontHeightInPoints((short)11); //字体加粗 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //设置字体名字 font.setFontName("Courier New"); //设置样式; HSSFCellStyle style = workbook.createCellStyle(); //设置底边框; style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //设置底边框颜色; style.setBottomBorderColor(HSSFColor.BLACK.index); //设置左边框; style.setBorderLeft(HSSFCellStyle.BORDER_THIN); //设置左边框颜色; style.setLeftBorderColor(HSSFColor.BLACK.index); //设置右边框; style.setBorderRight(HSSFCellStyle.BORDER_THIN); //设置右边框颜色; style.setRightBorderColor(HSSFColor.BLACK.index); //设置顶边框; style.setBorderTop(HSSFCellStyle.BORDER_THIN); //设置顶边框颜色; style.setTopBorderColor(HSSFColor.BLACK.index); //在样式用应用设置的字体; style.setFont(font); //设置自动换行; style.setWrapText(false); //设置水平对齐的样式为居中对齐; style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //设置垂直对齐的样式为居中对齐; style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); return style; } /* * 列数据信息单元格样式 */ public HSSFCellStyle getStyle(HSSFWorkbook workbook) { // 设置字体 HSSFFont font = workbook.createFont(); //设置字体大小 //font.setFontHeightInPoints((short)10); //字体加粗 //font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //设置字体名字 font.setFontName("Courier New"); //设置样式; HSSFCellStyle style = workbook.createCellStyle(); //设置底边框; style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //设置底边框颜色; style.setBottomBorderColor(HSSFColor.BLACK.index); //设置左边框; style.setBorderLeft(HSSFCellStyle.BORDER_THIN); //设置左边框颜色; style.setLeftBorderColor(HSSFColor.BLACK.index); //设置右边框; style.setBorderRight(HSSFCellStyle.BORDER_THIN); //设置右边框颜色; style.setRightBorderColor(HSSFColor.BLACK.index); //设置顶边框; style.setBorderTop(HSSFCellStyle.BORDER_THIN); //设置顶边框颜色; style.setTopBorderColor(HSSFColor.BLACK.index); //在样式用应用设置的字体; style.setFont(font); //设置自动换行; style.setWrapText(false); //设置水平对齐的样式为居中对齐; style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //设置垂直对齐的样式为居中对齐; style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); return style; } }

Ⅱ 如何用JAVA导出Excel(使用POI)

response.setContentType("bin");response.setHeader("Content-disposition","attachment;filename=test.xls");你在servlet里面设置响应头为这样,然后就能够实现下载了,然后使用输出流进行输出下载…..还有问题的话就问.. 你要是想全部代码都写的话..加我扣扣 1195391953..

Ⅲ java poi 在服务器生成excel文件

报格式错误是因为你没有填充EXCEL的内容。正确的做法是:1, HSSFWorkbook ws = new HSSFWorkbook();//建立新HSSFWorkbook对象2, Sheet sheet = workbook.createSheet(0); //建立一个新的sheet3,Row row = sheet.createRow(1); //建立一个新的row对象4, Cell cell = row.createCell(0); //在row上创建方格即列, cell.setCellValue(cellValue); //设置这个行中列的值 cell.setCellStyle(cellStyle); //设置样式

Ⅳ Java-怎么才能用Poi打开已有的excel文件,然后追加一些单元格阿

String filename = "excel名字.xls";res.setHeader("Content-Type", "application/vnd.ms-excel");res.setHeader("ContentDisposition", "attachment;filename=" + new String(filename.getBytes("MS932"), "ISO-8859-1"));调用一个方法,输出要输出的值,hm是个mapoutExcel(hm, res.getOutputStream());别看下面的方法代码多,其实有用的就几行,主要就是控制row和cell,设置单元格样式,赋值。private void outExcel(Map hm, ServletOutputStream out) throws IOException, Exception { String excelPath = excel模板路径; excelPath = new String(excelPath.getBytes("iso-8859-1"), "utf-8"); POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(excelPath)); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet1 = wb.getSheetAt(0); wb.setSheetName(0, "SheetName"); HSSFPrintSetup ps1 = sheet1.getPrintSetup(); ps1.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); HSSFRow row = sheet1.getRow((short) (1)); HSSFCell cell = row.getCell((short) (1)); String dataNumber = (String) hm.get("dataNumber"); cell.setCellValue(new HSSFRichTextString(dataNumber)); String birthAddress = (String) hm.get("birthAddress"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(birthAddress)); String acceptDate = (String) hm.get("acceptDate"); row = sheet1.getRow((short) (2)); cell = row.getCell((short) (1)); cell.setCellValue(new HSSFRichTextString(acceptDate)); String usedistrictCode = (String) hm.get("usedistrictCode"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(usedistrictCode)); String allegationZipCode = (String) hm.get("allegationZipCode"); row = sheet1.getRow((short) (3)); cell = row.getCell((short) (1)); cell.setCellValue(new HSSFRichTextString(allegationZipCode)); String birthName = (String) hm.get("birthName"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(birthName)); String allegationAddress = (String) hm.get("allegationAddress"); row = sheet1.getRow((short) (4)); cell = row.getCell((short) (1)); cell.setCellValue(new HSSFRichTextString(allegationAddress)); String birthTellNumber = (String) hm.get("birthTellNumber"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(birthTellNumber)); String allegationName = (String) hm.get("allegationName"); row = sheet1.getRow((short) (5)); cell = row.getCell((short) (1)); cell.setCellValue(new HSSFRichTextString(allegationName)); // 敪惗尮娆庬僐乕僪 String instrialCode = (String) hm.get("instrialCode"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(instrialCode)); // 怽棫幰背榖斣崋 String allegationTellNumber = (String) hm.get("allegationTellNumber"); row = sheet1.getRow((short) (6)); cell = row.getCell((short) (1)); cell.setCellValue(new HSSFRichTextString(allegationTellNumber)); // 敪惗尮巤愝摍 String birthFacilitiesetc = (String) hm.get("birthFacilitiesetc"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(birthFacilitiesetc)); // 怽棫撪梕 String allegation = (String) hm.get("allegation"); row = sheet1.getRow((short) (7)); cell = row.getCell((short) (1)); cell.setCellValue(new HSSFRichTextString(allegation)); // 挷嵏巜摫撪梕 String guidance = (String) hm.get("guidance"); row = sheet1.getRow((short) (8)); cell = row.getCell((short) (1)); cell.setCellValue(new HSSFRichTextString(guidance)); // 应掕擭宁担1 String sokuDate1 = (String) hm.get("sokuDate1"); row = sheet1.getRow((short) (9)); cell = row.getCell((short) (2)); cell.setCellValue(new HSSFRichTextString(sokuDate1)); // 应掕擭宁担2 String sokuDate2 = (String) hm.get("sokuDate2"); cell = row.getCell((short) (3)); cell.setCellValue(new HSSFRichTextString(sokuDate2)); // 应掕擭宁担3 String sokuDate3 = (String) hm.get("sokuDate3"); cell = row.getCell((short) (4)); cell.setCellValue(new HSSFRichTextString(sokuDate3)); // 应掕擭宁担4 String sokuDate4 = (String) hm.get("sokuDate4"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(sokuDate4)); // 应掕擭宁担5 String sokuDate5 = (String) hm.get("sokuDate5"); cell = row.getCell((short) (6)); cell.setCellValue(new HSSFRichTextString(sokuDate5)); // 应掕擭宁担6 String sokuDate6 = (String) hm.get("sokuDate6"); cell = row.getCell((short) (7)); cell.setCellValue(new HSSFRichTextString(sokuDate6)); // 应掕帪岗1 String sokuTime1 = (String) hm.get("sokuTime1"); row = sheet1.getRow((short) (10)); cell = row.getCell((short) (2)); cell.setCellValue(new HSSFRichTextString(sokuTime1)); // 应掕帪岗2 String sokuTime2 = (String) hm.get("sokuTime2"); cell = row.getCell((short) (3)); cell.setCellValue(new HSSFRichTextString(sokuTime2)); // 应掕帪岗3 String sokuTime3 = (String) hm.get("sokuTime3"); cell = row.getCell((short) (4)); cell.setCellValue(new HSSFRichTextString(sokuTime3)); // 应掕帪岗4 String sokuTime4 = (String) hm.get("sokuTime4"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(sokuTime4)); // 应掕帪岗5 String sokuTime5 = (String) hm.get("sokuTime5"); cell = row.getCell((short) (6)); cell.setCellValue(new HSSFRichTextString(sokuTime5)); // 应掕帪岗6 String sokuTime6 = (String) hm.get("sokuTime6"); cell = row.getCell((short) (7)); cell.setCellValue(new HSSFRichTextString(sokuTime6)); // 帋桠斣崋1 String shiryoNumber1 = (String) hm.get("shiryoNumber1"); row = sheet1.getRow((short) (11)); cell = row.getCell((short) (2)); cell.setCellValue(new HSSFRichTextString(shiryoNumber1)); // 帋桠斣崋2 String shiryoNumber2 = (String) hm.get("shiryoNumber2"); cell = row.getCell((short) (3)); cell.setCellValue(new HSSFRichTextString(shiryoNumber2)); // 帋桠斣崋3 String shiryoNumber3 = (String) hm.get("shiryoNumber3"); cell = row.getCell((short) (4)); cell.setCellValue(new HSSFRichTextString(shiryoNumber3)); // 帋桠斣崋4 String shiryoNumber4 = (String) hm.get("shiryoNumber4"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(shiryoNumber4)); // 帋桠斣崋5 String shiryoNumber5 = (String) hm.get("shiryoNumber5"); cell = row.getCell((short) (6)); cell.setCellValue(new HSSFRichTextString(shiryoNumber5)); // 帋桠斣崋6 String shiryoNumber6 = (String) hm.get("shiryoNumber6"); cell = row.getCell((short) (7)); cell.setCellValue(new HSSFRichTextString(shiryoNumber6)); // 婥壏1 String temp1 = (String) hm.get("temp1"); row = sheet1.getRow((short) (12)); cell = row.getCell((short) (2)); cell.setCellValue(new HSSFRichTextString(temp1)); // 婥壏2 String temp2 = (String) hm.get("temp2"); cell = row.getCell((short) (3)); cell.setCellValue(new HSSFRichTextString(temp2)); // 婥壏3 String temp3 = (String) hm.get("temp3"); cell = row.getCell((short) (4)); cell.setCellValue(new HSSFRichTextString(temp3)); // 婥壏4 String temp4 = (String) hm.get("temp4"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(temp4)); // 婥壏5 String temp5 = (String) hm.get("temp5"); cell = row.getCell((short) (6)); cell.setCellValue(new HSSFRichTextString(temp5)); // 婥壏6 String temp6 = (String) hm.get("temp6"); cell = row.getCell((short) (7)); cell.setCellValue(new HSSFRichTextString(temp6)); // 幖搙1 String humid1 = (String) hm.get("humid1"); row = sheet1.getRow((short) (13)); cell = row.getCell((short) (2)); cell.setCellValue(new HSSFRichTextString(humid1)); // 幖搙2 String humid2 = (String) hm.get("humid2"); cell = row.getCell((short) (3)); cell.setCellValue(new HSSFRichTextString(humid2)); // 幖搙3 String humid3 = (String) hm.get("humid3"); cell = row.getCell((short) (4)); cell.setCellValue(new HSSFRichTextString(humid3)); // 幖搙4 String humid4 = (String) hm.get("humid4"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(humid4)); // 幖搙5 String humid5 = (String) hm.get("humid5"); cell = row.getCell((short) (6)); cell.setCellValue(new HSSFRichTextString(humid5)); // 幖搙6 String humid6 = (String) hm.get("humid6"); cell = row.getCell((short) (7)); cell.setCellValue(new HSSFRichTextString(humid6)); // 晽岦1 String windCode1 = (String) hm.get("windCode1"); row = sheet1.getRow((short) (14)); cell = row.getCell((short) (2)); cell.setCellValue(new HSSFRichTextString(windCode1)); // 晽岦2 String windCode2 = (String) hm.get("windCode2"); cell = row.getCell((short) (3)); cell.setCellValue(new HSSFRichTextString(windCode2)); String windCode3 = (String) hm.get("windCode3"); cell = row.getCell((short) (4)); cell.setCellValue(new HSSFRichTextString(windCode3)); String windCode4 = (String) hm.get("windCode4"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(windCode4)); String windCode5 = (String) hm.get("windCode5"); cell = row.getCell((short) (6)); cell.setCellValue(new HSSFRichTextString(windCode5)); String windCode6 = (String) hm.get("windCode6"); cell = row.getCell((short) (7)); cell.setCellValue(new HSSFRichTextString(windCode6)); String windSpeed1 = (String) hm.get("windSpeed1"); row = sheet1.getRow((short) (15)); cell = row.getCell((short) (2)); cell.setCellValue(new HSSFRichTextString(windSpeed1)); String windSpeed2 = (String) hm.get("windSpeed2"); cell = row.getCell((short) (3)); cell.setCellValue(new HSSFRichTextString(windSpeed2)); String windSpeed3 = (String) hm.get("windSpeed3"); cell = row.getCell((short) (4)); cell.setCellValue(new HSSFRichTextString(windSpeed3)); String windSpeed4 = (String) hm.get("windSpeed4"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(windSpeed4)); String windSpeed5 = (String) hm.get("windSpeed5"); cell = row.getCell((short) (6)); cell.setCellValue(new HSSFRichTextString(windSpeed5)); String windSpeed6 = (String) hm.get("windSpeed6"); cell = row.getCell((short) (7)); cell.setCellValue(new HSSFRichTextString(windSpeed6)); String pick1 = (String) hm.get("pick1"); row = sheet1.getRow((short) (16)); cell = row.getCell((short) (2)); cell.setCellValue(new HSSFRichTextString(pick1)); String pick2 = (String) hm.get("pick2"); cell = row.getCell((short) (3)); cell.setCellValue(new HSSFRichTextString(pick2)); String pick3 = (String) hm.get("pick3"); cell = row.getCell((short) (4)); cell.setCellValue(new HSSFRichTextString(pick3)); String pick4 = (String) hm.get("pick4"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(pick4)); String pick5 = (String) hm.get("pick5"); cell = row.getCell((short) (6)); cell.setCellValue(new HSSFRichTextString(pick5)); String pick6 = (String) hm.get("pick6"); cell = row.getCell((short) (7)); cell.setCellValue(new HSSFRichTextString(pick6)); String smell1 = (String) hm.get("smell1"); row = sheet1.getRow((short) (17)); cell = row.getCell((short) (2)); cell.setCellValue(new HSSFRichTextString(smell1)); String smell2 = (String) hm.get("smell2"); cell = row.getCell((short) (3)); cell.setCellValue(new HSSFRichTextString(smell2)); String smell3 = (String) hm.get("smell3"); cell = row.getCell((short) (4)); cell.setCellValue(new HSSFRichTextString(smell3)); String smell4 = (String) hm.get("smell4"); cell = row.getCell((short) (5)); cell.setCellValue(new HSSFRichTextString(smell4)); String smell5 = (String) hm.get("smell5"); cell = row.getCell((short) (6)); cell.setCellValue(new HSSFRichTextString(smell5)); String smell6 = (String) hm.get("smell6"); cell = row.getCell((short) (7)); cell.setCellValue(new HSSFRichTextString(smell6)); wb.write(out); out.close();}

Ⅳ java操作poi怎么更改excel中的数据

修改完需要写入,也就是保存一下的。import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException; import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class ChangeCell { @SuppressWarnings("deprecation") public static void main(String[] args) { String fileToBeRead = "C:\\exp.xls"; // excel位置 int coloum = 1; // 比如你要获取第1列 try { HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream( fileToBeRead)); HSSFSheet sheet = workbook.getSheet("Sheet1"); for (int i = 0; i <= sheet.getLastRowNum(); i++) { HSSFRow row = sheet.getRow((short) i); if (null == row) { continue; } else { HSSFCell cell = row.getCell((short) coloum); if (null == cell) { continue; } else { System.out.println(cell.getNumericCellValue()); int temp = (int) cell.getNumericCellValue(); cell.setCellValue(temp + 1); } } } FileOutputStream out = null; try { out = new FileOutputStream(fileToBeRead); workbook.write(out); } catch (IOException e) { e.printStackTrace(); } finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

Ⅵ java用poi导出excel文件,打开导出的文件时报错,怎么办

两个原因:

1.你的excel模版本身有问题,可以尝试新建一个模版。

2.你的excel使用了一些POI不支持的函数。

解决办法:

另存是由excel重写了完整的文件,可以解决问题。

关闭文件例子:

FileOutputStream os = new FileOutputStream("workbook.xls");

wb.write(os);

os.close();

Ⅶ java使用poi导出excel

找不到文件流,原因是因为,这里的inputName是你本机的路径,你本机有这个文回件,所以能下载答,但是发布到服务器上,这里的inputName就是服务器上的路径,而服务器上这个路径下没有这个文件,所以找不到文件流!

Ⅷ javapoi数据导出成excel如何才能指定文件输出路径 现在是知道E盘路径 怎么弄成弹框选择路径

点导出的时候,把所需要的数据传入导出界面JSP,这是我自己拼接的,代码如下<%@ page contentType="application/vnd.ms-excel; charset=utf-8" %><%@ page language="java" import="java.util.*,com.expect.bean.Admin" pageEncoding="UTF-8"%><% String path = request.getContextPath(); String xlsname = request.getAttribute("xlsname").toString(); String fileName = xlsname+".xls"; byte[] yte = fileName.getBytes("GB2312"); String unicoStr = new String(yte, "ISO-8859-1"); response.setHeader("Content-disposition","attachment;filename="+unicoStr);%><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><style>#tableinfo tr td{text-align: center;font-size: 13px;border: 1px solid #64ABD6;}#tableinfo{width: 100%;border-collapse: collapse;height: 30px;line-height: 30px;}</style></head><body ><table cellspacing="0" id="tableinfo" ><tr><td style="text-align: center;font-size:30px;font-weight: bold;" id="titletd" colspan="8"><%=xlsname %>电费表<br/></td></tr><tr><td style="text-align: center;" id="titletd" colspan="2">用户名:</td><td style="text-align: center;" id="titletd"> 分厂:</td><td style="text-align: center;" id="titletd"> 住宅区:</td><td style="text-align: center;" id="titletd" >楼层:</td><td style="text-align: center;" id="titletd" > 具体住址:</td><td style="text-align: center;" id="titletd" > 上次度数:</td><td style="text-align: center;" id="titletd" > 本次度数:</td><td style="text-align: center;" id="titletd" > 操作时间:</td></tr></table><%List list = (List)request.getAttribute("list");if(list!=null && list.size()>0){for(int i=0;i<list.size();i++){Map m = (Map)list.get(i);String userName = m.get("userName")==null || m.get("userName").equals("") ? "" : m.get("userName").toString();String branch = m.get("branch")==null || m.get("branch").equals("") ? "" : m.get("branch").toString();String upTown = m.get("upTown")==null || m.get("upTown").equals("") ? "" : m.get("upTown").toString();String storey = m.get("storey")==null || m.get("storey").equals("") ? "" : m.get("storey").toString();String home = m.get("home")==null || m.get("home").equals("") ? "" : m.get("home").toString();String totalSum = m.get("totalSum")==null || m.get("totalSum").equals("") ? "" : m.get("totalSum").toString();String enterTime = m.get("enterTime")==null || m.get("enterTime").equals("") ? "" : m.get("enterTime").toString();String upMonthCount = m.get("upMonthCount")==null || m.get("upMonthCount").equals("") ? "" : m.get("upMonthCount").toString(); %><table cellspacing="0" id="tableinfo" ><tr><td style="text-align: center;" id="titletd" colspan="2"><%=userName %></td><td style="text-align: center;" id="titletd" ><%=branch %></td><td style="text-align: center;" id="titletd" ><%=upTown %></td><td style="text-align: center;" id="titletd" ><%=storey %></td><td style="text-align: center;" id="titletd" ><%=home %></td><td style="text-align: center;" id="titletd" ><%=totalSum %></td><td style="text-align: center;" id="titletd" ><%=upMonthCount %></td><td style="text-align: center;" id="titletd" ><%=enterTime %></td></tr></table><%}} %></body></html>

Ⅸ 如何使用Java POI生成Excel表文件

()throwsIOException{Stringpath="C:\poi2.xlsx";XSSFWorkbookworkbook=newXSSFWorkbook();XSSFSheetsheet=workbook.createSheet("我的Sheet");XSSFRowrow=sheet.createRow(0);XSSFCellcell=row.createCell(0);cell.setCellValue("我是写入的");XSSFRowrow1=sheet.createRow(1);XSSFCellcell1=row1.createCell(0);cell1.setCellValue("2010");FileOutputStreamoutputStream=newFileOutputStream(path);workbook.write(outputStream);outputStream.close();}()throwsIOException{Stringpath="C:\poi2.xls";HSSFWorkbookworkbook=newHSSFWorkbook();HSSFSheetsheet=workbook.createSheet("我的Excel");HSSFRowrow=sheet.createRow(0);HSSFCellcell=row.createCell(0);cell.setCellValue("我是POI写入的");FileOutputStreamoutputStream=newFileOutputStream(path);workbook.write(outputStream);outputStream.close();}

Ⅹ java poi 写excel文件的问题

最后的tableCell没有指向后边的几个单元格啊。

未经允许不得转载:山九号 » javapoiexcel文件|java操作poi怎么更改excel中的数据

赞 (0)