org.apache.poi.xssf.usermodel.XSSFCell Java Examples
The following examples show how to use
org.apache.poi.xssf.usermodel.XSSFCell.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EpidemicReport.java From MyBox with Apache License 2.0 | 21 votes |
public static List<String> writeExcelHeader(XSSFWorkbook wb, XSSFSheet sheet, List<String> extraFields) { try { List<String> columns = externalNames(extraFields); sheet.setDefaultColumnWidth(20); XSSFRow titleRow = sheet.createRow(0); XSSFCellStyle horizontalCenter = wb.createCellStyle(); horizontalCenter.setAlignment(HorizontalAlignment.CENTER); for (int i = 0; i < columns.size(); i++) { XSSFCell cell = titleRow.createCell(i); cell.setCellValue(columns.get(i)); cell.setCellStyle(horizontalCenter); } return columns; } catch (Exception e) { return null; } }
Example #2
Source File: ObjectDataExportServiceImpl.java From axelor-open-suite with GNU Affero General Public License v3.0 | 8 votes |
private MetaFile writeExcel(Map<String, List<String[]>> data) throws IOException { XSSFWorkbook workBook = new XSSFWorkbook(); for (String model : data.keySet()) { XSSFSheet sheet = workBook.createSheet(model); int count = 0; for (String[] record : data.get(model)) { XSSFRow row = sheet.createRow(count); int cellCount = 0; for (String val : record) { XSSFCell cell = row.createCell(cellCount); cell.setCellValue(val); cellCount++; } count++; } } File excelFile = MetaFiles.createTempFile("Data", ".xls").toFile(); FileOutputStream out = new FileOutputStream(excelFile); workBook.write(out); out.close(); return metaFiles.upload(excelFile); }
Example #3
Source File: GeographyCode.java From MyBox with Apache License 2.0 | 6 votes |
public static List<String> writeExcelHeader(XSSFWorkbook wb, XSSFSheet sheet) { try { List<String> columns = externalNames(); sheet.setDefaultColumnWidth(20); XSSFRow titleRow = sheet.createRow(0); XSSFCellStyle horizontalCenter = wb.createCellStyle(); horizontalCenter.setAlignment(HorizontalAlignment.CENTER); for (int i = 0; i < columns.size(); i++) { XSSFCell cell = titleRow.createCell(i); cell.setCellValue(columns.get(i)); cell.setCellStyle(horizontalCenter); } return columns; } catch (Exception e) { return null; } }
Example #4
Source File: ExcelHandle.java From danyuan-application with Apache License 2.0 | 6 votes |
@SuppressWarnings("deprecation") public void readXLSX(String path, int num) throws InvalidFormatException, IOException { File file = new File(path); @SuppressWarnings("resource") XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(file)); XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(num); int rowstart = xssfSheet.getFirstRowNum(); int rowEnd = xssfSheet.getLastRowNum(); for (int i = rowstart; i <= rowEnd; i++) { XSSFRow row = xssfSheet.getRow(i); if (null == row) { continue; } int cellStart = row.getFirstCellNum(); int cellEnd = row.getLastCellNum(); for (int k = cellStart; k <= cellEnd; k++) { XSSFCell cell = row.getCell(k); if (null == cell) { continue; } switch (cell.getCellTypeEnum()) { case NUMERIC: // 数字 System.out.print(cell.getNumericCellValue() + " "); break; case STRING: // 字符串 System.out.print(cell.getStringCellValue() + " "); break; case BOOLEAN: // Boolean System.out.println(cell.getBooleanCellValue() + " "); break; case FORMULA: // 公式 System.out.print(cell.getCellFormula() + " "); break; case BLANK: // 空值 System.out.println(" "); break; case ERROR: // 故障 System.out.println(" "); break; default: System.out.print("未知类型 "); break; } } System.out.print("\n"); } }
Example #5
Source File: ExcelServiceImpl.java From agile-service-old with Apache License 2.0 | 6 votes |
private Integer getRealRowCount(Sheet sheet) { Integer count = 0; for (int r = 1; r <= sheet.getPhysicalNumberOfRows(); r++) { Row row = sheet.getRow(r); if (row == null || (((row.getCell(0) == null || row.getCell(0).toString().equals("") || row.getCell(0).getCellType() == XSSFCell.CELL_TYPE_BLANK)) && (row.getCell(1) == null || row.getCell(1).toString().equals("") || row.getCell(1).getCellType() == XSSFCell.CELL_TYPE_BLANK) && (row.getCell(2) == null || row.getCell(2).toString().equals("") || row.getCell(2).getCellType() == XSSFCell.CELL_TYPE_BLANK) && (row.getCell(3) == null || row.getCell(3).toString().equals("") || row.getCell(3).getCellType() == XSSFCell.CELL_TYPE_BLANK) && (row.getCell(4) == null || row.getCell(4).toString().equals("") || row.getCell(4).getCellType() == XSSFCell.CELL_TYPE_BLANK) && (row.getCell(5) == null || row.getCell(5).toString().equals("") || row.getCell(5).getCellType() == XSSFCell.CELL_TYPE_BLANK) && (row.getCell(6) == null || row.getCell(6).toString().equals("") || row.getCell(6).getCellType() == XSSFCell.CELL_TYPE_BLANK) && (row.getCell(7) == null || row.getCell(7).toString().equals("") || row.getCell(7).getCellType() == XSSFCell.CELL_TYPE_BLANK) && (row.getCell(8) == null || row.getCell(8).toString().equals("") || row.getCell(8).getCellType() == XSSFCell.CELL_TYPE_BLANK) && (row.getCell(9) == null || row.getCell(9).toString().equals("") || row.getCell(9).getCellType() == XSSFCell.CELL_TYPE_BLANK))) { continue; } count++; } return count; }
Example #6
Source File: PoiXSSFExcelUtil.java From JavaWeb with Apache License 2.0 | 6 votes |
private static List<List<String>> readSheet(XSSFSheet xssfSheet){ int rows = xssfSheet.getPhysicalNumberOfRows(); List<List<String>> rowList = new ArrayList<>(); for(int i=0;i<rows;i++){//遍历每一行 XSSFRow row = xssfSheet.getRow(i); if(row==null){ continue; } int cells = row.getPhysicalNumberOfCells(); List<String> cellList = new ArrayList<>(); for(int j=0;j<cells;j++){//遍历每一列 XSSFCell cell = row.getCell(j); cell.setCellType(Cell.CELL_TYPE_STRING); //new Double("1.0").intValue() String cellValue = cell.getStringCellValue(); cellList.add(cellValue); } rowList.add(cellList); } return rowList; }
Example #7
Source File: ExcelServices.java From M2Doc with Eclipse Public License 1.0 | 6 votes |
/** * Gets the {@link MStyle} of the given {@link XSSFCell}. * * @param cellthe * {@link XSSFCell} * @return the {@link MStyle} of the given {@link XSSFCell} */ private MStyle getStyle(XSSFCell cell) { final XSSFCellStyle style = cell.getCellStyle(); final XSSFFont font = style.getFont(); int modifiers = 0; if (font.getBold()) { modifiers |= MStyle.FONT_BOLD; } if (font.getItalic()) { modifiers |= MStyle.FONT_ITALIC; } if (font.getStrikeout()) { modifiers |= MStyle.FONT_STRIKE_THROUGH; } if (font.getUnderline() != 0) { modifiers |= MStyle.FONT_UNDERLINE; } return new MStyleImpl(font.getFontName(), font.getFontHeightInPoints(), getColor(font.getXSSFColor()), null, modifiers); }
Example #8
Source File: ExcelDataProvider.java From xframium-java with GNU General Public License v3.0 | 6 votes |
/** * Gets the cell value. * * @param cell the cell * @return the cell value */ private String getCellValue( XSSFCell cell ) { if (cell != null) { switch (cell.getCellType()) { case XSSFCell.CELL_TYPE_BLANK: return null; case XSSFCell.CELL_TYPE_BOOLEAN: return String.valueOf( cell.getBooleanCellValue() ); case XSSFCell.CELL_TYPE_NUMERIC: return String.valueOf( ( int ) cell.getNumericCellValue() ); case XSSFCell.CELL_TYPE_STRING: return cell.getRichStringCellValue().toString(); } } return null; }
Example #9
Source File: ExcelCloudProvider.java From xframium-java with GNU General Public License v3.0 | 6 votes |
/** * Gets the cell value. * * @param cell the cell * @return the cell value */ private String getCellValue( XSSFCell cell ) { if (cell != null) { switch (cell.getCellType()) { case XSSFCell.CELL_TYPE_BLANK: return null; case XSSFCell.CELL_TYPE_BOOLEAN: return String.valueOf( cell.getBooleanCellValue() ); case XSSFCell.CELL_TYPE_NUMERIC: return String.valueOf( cell.getNumericCellValue() ); case XSSFCell.CELL_TYPE_STRING: return cell.getRichStringCellValue().toString(); } } return null; }
Example #10
Source File: ExcelApplicationProvider.java From xframium-java with GNU General Public License v3.0 | 6 votes |
/** * Gets the cell value. * * @param cell the cell * @return the cell value */ private String getCellValue( XSSFCell cell ) { if (cell != null) { switch (cell.getCellType()) { case XSSFCell.CELL_TYPE_BLANK: return null; case XSSFCell.CELL_TYPE_BOOLEAN: return String.valueOf( cell.getBooleanCellValue() ); case XSSFCell.CELL_TYPE_NUMERIC: return String.valueOf( cell.getNumericCellValue() ); case XSSFCell.CELL_TYPE_STRING: return cell.getRichStringCellValue().toString(); } } return null; }
Example #11
Source File: ExcelContentProvider.java From xframium-java with GNU General Public License v3.0 | 6 votes |
/** * Gets the cell value. * * @param cell the cell * @return the cell value */ private String getCellValue( XSSFCell cell ) { if (cell != null) { switch (cell.getCellType()) { case XSSFCell.CELL_TYPE_BLANK: return null; case XSSFCell.CELL_TYPE_BOOLEAN: return String.valueOf( cell.getBooleanCellValue() ); case XSSFCell.CELL_TYPE_NUMERIC: return String.valueOf( cell.getNumericCellValue() ); case XSSFCell.CELL_TYPE_STRING: return cell.getRichStringCellValue().toString(); } } return null; }
Example #12
Source File: ExcelElementProvider.java From xframium-java with GNU General Public License v3.0 | 6 votes |
/** * Gets the cell value. * * @param cell the cell * @return the cell value */ private String getCellValue( XSSFCell cell ) { if (cell != null) { switch (cell.getCellType()) { case XSSFCell.CELL_TYPE_BLANK: return null; case XSSFCell.CELL_TYPE_BOOLEAN: return String.valueOf( cell.getBooleanCellValue() ); case XSSFCell.CELL_TYPE_NUMERIC: return String.valueOf( cell.getNumericCellValue() ); case XSSFCell.CELL_TYPE_STRING: return cell.getRichStringCellValue().toString(); } } return null; }
Example #13
Source File: ExcelPageDataProvider.java From xframium-java with GNU General Public License v3.0 | 6 votes |
/** * Gets the cell value. * * @param cell the cell * @return the cell value */ private String getCellValue( XSSFCell cell ) { if (cell != null) { switch (cell.getCellType()) { case XSSFCell.CELL_TYPE_BLANK: return null; case XSSFCell.CELL_TYPE_BOOLEAN: return String.valueOf( cell.getBooleanCellValue() ); case XSSFCell.CELL_TYPE_NUMERIC: { String useValue = String.valueOf( cell.getNumericCellValue() ); if ( useValue.endsWith( ".0" ) ) return useValue.split( "\\." )[0]; else return useValue; } case XSSFCell.CELL_TYPE_STRING: return cell.getRichStringCellValue().toString(); } } return null; }
Example #14
Source File: ExcelKeyWordProvider.java From xframium-java with GNU General Public License v3.0 | 6 votes |
private String getCellValue( XSSFCell cell ) { if (cell != null ) { switch (cell.getCellType()) { case XSSFCell.CELL_TYPE_BLANK: return null; case XSSFCell.CELL_TYPE_BOOLEAN: return String.valueOf( cell.getBooleanCellValue() ); case XSSFCell.CELL_TYPE_NUMERIC: return String.valueOf( cell.getNumericCellValue() ); case XSSFCell.CELL_TYPE_STRING: return cell.getRichStringCellValue().toString(); } } return null; }
Example #15
Source File: PoiXSSFExcelUtil.java From JavaWeb with Apache License 2.0 | 6 votes |
private static XSSFWorkbook writeSheetData(List<List<String>> data,String sheetName){ XSSFWorkbook xssfWorkbook = new XSSFWorkbook(); XSSFSheet xssfSheet = xssfWorkbook.createSheet(sheetName); XSSFRow[] rows = new XSSFRow[data.size()]; for(int i=0;i<data.size();i++){ List<String> columns = data.get(i); rows[i] = xssfSheet.createRow(i); //xssfSheet.setDefaultColumnWidth(columnWidth);//设置列的长度 XSSFCell[] cells = new XSSFCell[columns.size()]; for(int j=0;j<columns.size();j++){ cells[j] = rows[i].createCell(j); //cells[j].setCellStyle(XSSFCellStyle); cells[j].setCellValue(columns.get(j)); } } return xssfWorkbook; }
Example #16
Source File: TestExportExcel2007.java From poi with Apache License 2.0 | 6 votes |
/** * XSSFRichTextString.utfDecode()<br/> * value.contains("_x")<br/> * Pattern.compile("_x([0-9A-Fa-f]{4})_"); */ private static void export2007WithStyle(String filePath) { try { OutputStream os = new FileOutputStream(filePath); XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = wb.createSheet(Globals.SHEETNAME); XSSFCell cell = sheet.createRow(0).createCell(0); cell.setCellValue(TestUtil.RICH_TEXT_STRINGS[0] + escape(TestUtil.REGEX + TestUtil.RICH_TEXT_STRINGS[1] + TestUtil.REGEX) + TestUtil.RICH_TEXT_STRINGS[2]); CellStyle style = sheet.getWorkbook().createCellStyle(); XSSFFont font = wb.createFont(); font.setColor(IndexedColors.BLUE.index); style.setFont(font); cell.setCellStyle(style); // richString.applyFont(font); wb.write(os); os.close(); } catch (Exception e) { e.printStackTrace(); } }
Example #17
Source File: ExcelComparator.java From data-prep with Apache License 2.0 | 6 votes |
public static boolean compareTwoRows(XSSFRow row1, XSSFRow row2) { if ((row1 == null) && (row2 == null)) { return true; } else if ((row1 == null) || (row2 == null)) { return false; } int firstCell1 = row1.getFirstCellNum(); int lastCell1 = row1.getLastCellNum(); boolean equalRows = true; // Compare all cells in a row for (int i = firstCell1; i <= lastCell1; i++) { XSSFCell cell1 = row1.getCell(i); XSSFCell cell2 = row2.getCell(i); if (!compareTwoCells(cell1, cell2)) { equalRows = false; break; } } return equalRows; }
Example #18
Source File: XlsxResource.java From nextreports-server with Apache License 2.0 | 6 votes |
@Override protected void printHeader(List<String> header, ByteArrayOutputStream out) { wb = new XSSFWorkbook(); sheet = wb.createSheet("NextReports"); XSSFRow headerRow = sheet.createRow(0); int col = 0; if (header != null) { for (String s : header) { XSSFCell cell = headerRow.createCell(col); cell.setCellType(XSSFCell.CELL_TYPE_STRING); if (s == null) { s = ""; } cell.setCellValue(wb.getCreationHelper().createRichTextString(s)); col++; } } }
Example #19
Source File: TestExternalRelvarXLSX4.java From Rel with Apache License 2.0 | 5 votes |
@Before public void testXLS1() throws IOException { try (XSSFWorkbook workbook = new XSSFWorkbook()) { XSSFSheet sheet = workbook.createSheet(); XSSFRow row = null; XSSFCell cell = null; row = sheet.createRow(0); cell = row.createCell(0); cell.setCellValue("A"); cell = row.createCell(1); cell.setCellValue("B"); cell = row.createCell(2); cell.setCellValue("C"); insert(1,sheet,row,cell,1,2,3); insert(2,sheet,row,cell,4,5,6); insert(3,sheet,row,cell,7,8,9); try (FileOutputStream out = new FileOutputStream(file)) { workbook.write(out); } catch (IOException e) { e.printStackTrace(); } } String src = "BEGIN;\n" + "var myvar external xls \"" + file.getAbsolutePath() + "\";" + "END;\n" + "true"; testEquals("true", src); }
Example #20
Source File: TestExternalRelvarXLSX1.java From Rel with Apache License 2.0 | 5 votes |
@Before public void testXLS1() throws IOException { try (XSSFWorkbook workbook = new XSSFWorkbook()) { XSSFSheet sheet = workbook.createSheet(); XSSFRow row = null; XSSFCell cell = null; row = sheet.createRow(0); cell = row.createCell(0); cell.setCellValue("A"); cell = row.createCell(1); cell.setCellValue("B"); cell = row.createCell(2); cell.setCellValue("C"); insert(1,sheet,row,cell,1,2,3); insert(2,sheet,row,cell,4,5,6); insert(3,sheet,row,cell,7,8,9); try (FileOutputStream out = new FileOutputStream(file)) { workbook.write(out); } catch (IOException e) { e.printStackTrace(); } } String src = "BEGIN;\n" + "var myvar external xls \"" + file.getAbsolutePath() + "\" autokey;" + "END;\n" + "true"; testEquals("true", src); }
Example #21
Source File: TestExternalRelvarXLSX1.java From Rel with Apache License 2.0 | 5 votes |
private static void insert(int rowNum, XSSFSheet sheet, XSSFRow row, XSSFCell cell, int arg0, int arg1, int arg2) { row = sheet.createRow(rowNum); cell = row.createCell(0); cell.setCellValue(arg0); cell = row.createCell(1); cell.setCellValue(arg1); cell = row.createCell(2); cell.setCellValue(arg2); }
Example #22
Source File: TestExternalRelvarXLSX2.java From Rel with Apache License 2.0 | 5 votes |
private static void insert(int rowNum, XSSFSheet sheet, XSSFRow row, XSSFCell cell, int arg0, int arg1, int arg2) { row = sheet.createRow(rowNum); cell = row.createCell(0); cell.setCellValue(arg0); cell = row.createCell(1); cell.setCellValue(arg1); cell = row.createCell(2); cell.setCellValue(arg2); }
Example #23
Source File: TestExternalRelvarXLSX3.java From Rel with Apache License 2.0 | 5 votes |
private static void insert(int rowNum, XSSFSheet sheet, XSSFRow row, XSSFCell cell, int arg0, int arg1, int arg2) { row = sheet.createRow(rowNum); cell = row.createCell(0); cell.setCellValue(arg0); cell = row.createCell(1); cell.setCellValue(arg1); cell = row.createCell(2); cell.setCellValue(arg2); }
Example #24
Source File: TestExternalRelvarXLSX2.java From Rel with Apache License 2.0 | 5 votes |
@Before public void testXLS1() throws IOException { try (XSSFWorkbook workbook = new XSSFWorkbook()) { XSSFSheet sheet = workbook.createSheet(); XSSFRow row = null; XSSFCell cell = null; row = sheet.createRow(0); cell = row.createCell(0); cell.setCellValue("A"); cell = row.createCell(1); cell.setCellValue("B"); cell = row.createCell(2); cell.setCellValue("C"); insert(1,sheet,row,cell,1,2,3); insert(2,sheet,row,cell,4,5,6); insert(3,sheet,row,cell,4,5,6); insert(4,sheet,row,cell,1,2,3); insert(5,sheet,row,cell,7,8,9); insert(6,sheet,row,cell,7,8,9); insert(7,sheet,row,cell,4,5,6); try (FileOutputStream out = new FileOutputStream(file)) { workbook.write(out); } catch (IOException e) { e.printStackTrace(); } } String src = "BEGIN;\n" + "var myvar external xls \"" + file.getAbsolutePath() + "\" dup_remove;" + "END;\n" + "true"; testEquals("true", src); }
Example #25
Source File: TestExternalRelvarXLSX3.java From Rel with Apache License 2.0 | 5 votes |
@Before public void testXLS1() throws IOException { try (XSSFWorkbook workbook = new XSSFWorkbook()) { XSSFSheet sheet = workbook.createSheet(); XSSFRow row = null; XSSFCell cell = null; row = sheet.createRow(0); cell = row.createCell(0); cell.setCellValue("A"); cell = row.createCell(1); cell.setCellValue("B"); cell = row.createCell(2); cell.setCellValue("C"); insert(1,sheet,row,cell,1,2,3); insert(2,sheet,row,cell,4,5,6); insert(3,sheet,row,cell,4,5,6); insert(4,sheet,row,cell,1,2,3); insert(5,sheet,row,cell,7,8,9); insert(6,sheet,row,cell,7,8,9); insert(7,sheet,row,cell,4,5,6); try (FileOutputStream out = new FileOutputStream(file)) { workbook.write(out); } catch (IOException e) { e.printStackTrace(); } } String src = "BEGIN;\n" + "var myvar external xls \"" + file.getAbsolutePath() + "\" dup_count;" + "END;\n" + "true"; testEquals("true", src); }
Example #26
Source File: Util.java From Knowage-Server with GNU Affero General Public License v3.0 | 5 votes |
/** * @param source * the sheet to copy. * @param destSheet * the sheet to create. * @param srcRow * the row to copy. * @param destRow * the row to create. * @param styleMap * */ private static void copyRow(HSSFSheet source, XSSFSheet destSheet, HSSFRow srcRow, XSSFRow destRow, List<CellStyle> styleMap) { Set<CellRangeAddressWrapper> mergedRegions = new TreeSet<CellRangeAddressWrapper>(); short dh = source.getDefaultRowHeight(); if (srcRow.getHeight() != dh) { destRow.setHeight(srcRow.getHeight()); } int j = srcRow.getFirstCellNum(); if (j < 0) { j = 0; } for (; j <= srcRow.getLastCellNum(); j++) { HSSFCell oldCell = srcRow.getCell(j); XSSFCell newCell = destRow.getCell(j); if (oldCell != null) { if (newCell == null) { newCell = destRow.createCell(j); } copyCell(oldCell, newCell, styleMap); CellRangeAddress mergedRegion = getMergedRegion(source, srcRow.getRowNum(), (short) oldCell.getColumnIndex()); if (mergedRegion != null) { CellRangeAddress newMergedRegion = new CellRangeAddress(mergedRegion.getFirstRow(), mergedRegion.getLastRow(), mergedRegion.getFirstColumn(), mergedRegion.getLastColumn()); CellRangeAddressWrapper wrapper = new CellRangeAddressWrapper(newMergedRegion); if (isNewMergedRegion(wrapper, mergedRegions)) { mergedRegions.add(wrapper); destSheet.addMergedRegion(wrapper.range); } } } } }
Example #27
Source File: ExcelNodeSerializer.java From dhis2-core with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override protected void startSerialize( RootNode rootNode, OutputStream outputStream ) throws Exception { workbook = new XSSFWorkbook(); sheet = workbook.createSheet( "Sheet1" ); XSSFFont boldFont = workbook.createFont(); boldFont.setBold( true ); XSSFCellStyle boldCellStyle = workbook.createCellStyle(); boldCellStyle.setFont( boldFont ); // build schema for ( Node child : rootNode.getChildren() ) { if ( child.isCollection() ) { if ( !child.getChildren().isEmpty() ) { Node node = child.getChildren().get( 0 ); XSSFRow row = sheet.createRow( 0 ); int cellIdx = 0; for ( Node property : node.getChildren() ) { if ( property.isSimple() ) { XSSFCell cell = row.createCell( cellIdx++ ); cell.setCellValue( property.getName() ); cell.setCellStyle( boldCellStyle ); } } } } } }
Example #28
Source File: Excel2007Parser.java From mdw with Apache License 2.0 | 5 votes |
private CellRangeAddress getRangeIfMerged(XSSFCell cell, CellRangeAddress[] mergedRanges) { for (int i = 0; i < mergedRanges.length; i++) { CellRangeAddress range = mergedRanges[i]; if (range.isInRange(cell.getRowIndex(), cell.getColumnIndex())) return range; } return null; }
Example #29
Source File: FileUtil.java From JavaWeb with Apache License 2.0 | 5 votes |
public static void writeExcel(HttpServletResponse response,List<String> list) throws Exception { response.setContentType("application/vnd.ms-excel");//文件格式,此处设置为excel response.setHeader("Content-Disposition","attachment;filename=file.xls");//此处设置了下载文件的默认名称 ServletOutputStream sos = response.getOutputStream(); //创建一个新的excel XSSFWorkbook wb = new XSSFWorkbook();//XSSFWorkbook /** * 采用现成Excel模板 * 用这种方式得先保证每个cell有值,不然会报空指针 * 有时我们用row.getCell(i)会得到null,那么此时就要用Iterator<Cell> it = row.cellIterator(); * XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(new File("D://a.xlsx"))); * XSSFSheet sheet = wb.getSheet("Sheet1"); * row[i] = sheet.getRow(i); * headerCell[j] = row[i].getCell(j); */ //创建sheet页 XSSFSheet sheet = wb.createSheet("sheet1");//sheet名 //创建行数 XSSFRow[] row = new XSSFRow[list.size()]; //插入数据 for (int i = 0; i < row.length; i++) { row[i] = sheet.createRow(i); sheet.setDefaultColumnWidth(30);//设置列的长度 String info[] = list.get(i).split(","); XSSFCell[] headerCell = new XSSFCell[info.length]; for (int j = 0; j < headerCell.length; j++) { headerCell[j] = row[i].createCell(j); headerCell[j].setCellValue(new XSSFRichTextString(info[j])); /**设置模板样式*/ //headerCell[j].setCellStyle(setStyle(wb)); } } wb.write(sos); wb.close(); sos.flush(); sos.close(); response.flushBuffer(); }
Example #30
Source File: GeographyCode.java From MyBox with Apache License 2.0 | 5 votes |
public static void writeExcel(XSSFSheet sheet, int i, GeographyCode code) { try { List<String> row = values(code); XSSFRow sheetRow = sheet.createRow(i + 1); for (int j = 0; j < row.size(); j++) { XSSFCell cell = sheetRow.createCell(j); cell.setCellValue(row.get(j)); } } catch (Exception e) { } }