Tag: apache poi

Excel读取错误:标头签名无效。 如何解决?

我从浏览器上传一个Excel文件。 我正在使用POI jar。 但是得到错误无效的头部签名; 阅读3255307777713450285,预计-2226271756974174256 在我已经使用的两个jsp文件之下:JSP 1: <form action="Upload.jsp" enctype="MULTIPART/FORM-DATA" method=post > <input type="file" name="filename" /> <input type="submit" value="Upload" /> </form> JSP 2:Upload.jsp try{ InputStream file = request.getInputStream(); POIFSFileSystem myFileSystem = new POIFSFileSystem(file); HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem); HSSFSheet mySheet = myWorkBook.getSheetAt(0); Iterator rowIter = mySheet.rowIterator(); rowIter.next(); while (rowIter.hasNext()) { HSSFRow myRow = (HSSFRow) rowIter.next(); […]

使用Apache POI将列标签插入到数据透视表中?

我已经使用Apache POI 3.11创build了一个数据透视表。 喜欢这个: FileInputStream file = new FileInputStream(new File(path+fname)); XSSFWorkbook workbook = new XSSFWorkbook(file); XSSFSheet sheet = workbook.getSheetAt(0); //area of pivot data AreaReference a=new AreaReference("A1:J4"); CellReference b=new CellReference("N5"); XSSFPivotTable pivotTable = sheet.createPivotTable(a,b); //insert row pivotTable.addRowLabel(3); pivotTable.addRowLabel(6); //insert column pivotTable.addColumnLabel(DataConsolidateFunction.COUNT, 5); //export FileOutputStream output_file = new FileOutputStream(new File(path+"POI_XLS_Pivot_Example.xlsx")); workbook.write(output_file);//write excel document to output stream output_file.close(); […]

使用Apache POI在Excel中使用千位分隔符格式化数字

我想格式化一些数字单元格,用千位分隔符作为逗号。 例如: 12 -> 12 1200 -> 1,200 12000 -> 12,000 12000000 -> 12,000,000 120000000 -> 120,000,000 我有以下代码。 我应该怎样使用formatStr ? 有一个简单的方法吗? 或者我必须检测零的数量才能产生像这样的#,###,### ? String formatStr = ""; HSSFCellStyle style = workbook.createCellStyle(); HSSFDataFormat format = workbook.createDataFormat(); style.setDataFormat(format.getFormat(formatStr)); cell.setCellStyle(style); cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); 请记住,我正在处理数字。 单元格types将是数字,而不是string。 更新

适用于所有单元格的Apache POI风格

Cell cell = row.createCell(1); cell.setCellValue(rdf.getEffectiveDate()); cell.getCellStyle().setDataFormat(HSSFDataFormat.getBuiltinFormat("d-mmm-yy")); cell = row.createCell(2); cell.setCellValue(rdf.getExpiryDate()); cell.getCellStyle().setDataFormat(HSSFDataFormat.getBuiltinFormat("d-mmm-yy")); row.createCell(3).setCellValue(rdf.getPremium()); row.createCell(4).setCellValue(rdf.getAccountNumber()); row.createCell(5).setCellValue(rdf.getLedgerName()); 我想在以上两列中应用date格式。 但它正在被应用到所有的细胞。 我怎样才能防止这一点。

Java POI:如何查找具有string值的Excel单元格并获取其位置(行)以使用该位置来查找另一个单元格

我在电子表格中寻找一个string为'Total'的单元格,然后使用该单元格的行来查找总是同一个单元格/列的另一个单元格中的总值(第0个单元格为0基于索引)。 我有以下代码,它没有错误(语法),但findCell方法不返回rowNum值: public static void main(String[] args) throws IOException{ String fileName = "C:\\file-path\\report.xls"; String cellContent = "Total"; int rownr=0, colnr = 10; InputStream input = new FileInputStream(fileName); HSSFWorkbook wb = new HSSFWorkbook(input); HSSFSheet sheet = wb.getSheetAt(0); rownr = findRow(sheet, cellContent); output(sheet, rownr, colnr); finish(); } private static void output(HSSFSheet sheet, int rownr, int colnr) { […]

总是在使用Apache poi的excel单元格中显示两个小数点

例如, XSSFCellStyle style=(XSSFCellStyle) workbook.createCellStyle(); style.setDataFormat(workbook.createDataFormat().getFormat("#.##")); productCell.setCellValue(12.4); productCell.setCellType(Cell.CELL_TYPE_NUMERIC); productCell.setCellStyle(style); 这在指定的单元格中显示12.4 。 应该是12.40 。 值12显示为12.这是非常不必要的。 如果该值为0 ,则显示一个点. 。 它应该始终显示两位十进制数字 – 0.00 ,在这种情况下,无论存储在单元格中的值如何。 如何强制excel始终显示数字单元格中的两位十进制数字? 我使用下列样式之一来显示数字单元格。 XSSFColor commonColor = new XSSFColor(new java.awt.Color(240, 240, 240)); XSSFColor cellBorderColour = new XSSFColor(new java.awt.Color(0, 76, 153)); Font font = workbook.createFont(); font.setBoldweight(Font.BOLDWEIGHT_BOLD); font.setColor(IndexedColors.DARK_BLUE.index); XSSFCellStyle style = (XSSFCellStyle) workbook.createCellStyle(); style.setFillForegroundColor(commonColor); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setAlignment(CellStyle.ALIGN_RIGHT); style.setFont(font); style.setBorderLeft(BorderStyle.HAIR); style.setBorderColor(XSSFCellBorder.BorderSide.LEFT, […]

ResultSet到Excel(* .xlsx)表使用Apache POI

我正在尝试使用Apache Poi将ResultSet写入Excel(* .xlsx)表 。 Office Excel中无效的表格对象错误 但是,即使它在没有任何错误的情况下写入Excel文件,但当我尝试在Office Excel 2013中打开它时,它将显示一个错误并删除表格对象以仅显示纯数据视图。 以下是使用此示例的粗略示例代码: public static void writeExcel(ResultSet rs, int sqliteRowCount, String dir) { System.out.println("Writing Excel(*.xlsx) File…"); XSSFWorkbook workbook = null; try { if (rs != null) { // Get ResultSet MetaData ResultSetMetaData rsmd = rs.getMetaData(); // Number of columns int numColumns = rsmd.getColumnCount(); // Number of rows // […]

文件Excel从Apache POI无法打开由Ms Excel(损坏)

我不知道为什么使用POI编写的文件无法由Excel 2013打开,但该文件仍然可以通过POI读取。 (单元格值可以更改) 这是来自文件的错误 这里是代码 FileInputStream fis = null; try { fis = new FileInputStream(fileUri); //not error at fileUri } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } String urii = fileUri.replace(".xls", "0.xls"); //not error File fisx = new File(urii); Workbook workbook = null; workbook = new HSSFWorkbook(fis); Sheet sheet = workbook.getSheetAt(0); […]

Java Apache POI Excel保存为PDF

如何将excel文件转换/保存为pdf ? 我正在使用java play framework来生成一些excel文件,现在需求更改为pdf 。 我不想重新编码一切。 有没有办法转换为pdf ? 我生成的excel文件来自一个模板; 我读了Excel模板文件,写入更改,并保存为新的Excel文件。 这样,模板不变。 它包含边框,图像和其他格式。

在Java中使用模板创buildExcel表格,新版本的Excel

我发现下面的代码来创build一个现有的格式与Excel格式的Excel表,并添加数据,并将其保存到一个新的文件 POIFSFileSystem fs = new POIFSFileSystem( new FileInputStream("template.xls")); HSSFWorkbook wb = new HSSFWorkbook(fs, true); Will load an xls, preserving its structure (macros included). You can then modify it, HSSFSheet sheet1 = wb.getSheet("Data"); … 然后保存。 FileOutputStream fileOut = new FileOutputStream("new.xls"); wb.write(fileOut); fileOut.close(); 这工作绝对好。 但我的问题是,我现在正在处理新版本的Excel。 所以我需要开发一个类似的代码来处理新版本的模板。 有人可以build议我怎么能做到这一点? 我尝试将HSSWorkbook更改为XSSFWorkbook。 但是XSSFWorkbook没有一个让我传递一个布尔值的构造函数。 也。 当我尝试它,它复制数据,但与数据的行不保留该模板具有的列的格式。