org.apache.poi.hssf.usermodel.HSSFRow Java Examples

The following examples show how to use org.apache.poi.hssf.usermodel.HSSFRow. 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: POIUtils.java    From ermaster-b with Apache License 2.0 6 votes vote down vote up
public static CellLocation findCell(HSSFSheet sheet, String[] strs) {
	for (int rowNum = sheet.getFirstRowNum(); rowNum < sheet
			.getLastRowNum() + 1; rowNum++) {
		HSSFRow row = sheet.getRow(rowNum);
		if (row == null) {
			continue;
		}

		for (int i = 0; i < strs.length; i++) {
			Integer colNum = findColumn(row, strs[i]);

			if (colNum != null) {
				return new CellLocation(rowNum, colNum.shortValue());
			}
		}
	}

	return null;
}
 
Example #2
Source File: XlsResource.java    From nextreports-server with Apache License 2.0 6 votes vote down vote up
@Override
protected void printHeader(List<String> header, ByteArrayOutputStream out) {
	wb = new HSSFWorkbook();
       sheet = wb.createSheet("NextReports");

       HSSFRow headerRow = sheet.createRow(0);
       int col = 0;        
	if (header != null) {
		for (String s : header) {
			HSSFCell cell = headerRow.createCell(col);
			cell.setCellType(HSSFCell.CELL_TYPE_STRING);
			if (s == null) {
				s = "";
			}
			cell.setCellValue(new HSSFRichTextString(s));
			col++;
		}
	}		
}
 
Example #3
Source File: Prd3899IT.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void testBug() throws ResourceException, IOException, ReportProcessingException {
  final MasterReport report = DebugReportRunner.parseGoldenSampleReport( "Prd-3889.prpt" );
  final ByteArrayOutputStream bout = new ByteArrayOutputStream();
  ExcelReportUtil.createXLS( report, bout );

  final HSSFWorkbook wb = new HSSFWorkbook( new ByteArrayInputStream( bout.toByteArray() ) );
  final HSSFSheet sheetAt = wb.getSheetAt( 0 );
  final HSSFRow row = sheetAt.getRow( 0 );
  final HSSFCell cell0 = row.getCell( 0 );

  // assert that we are in the correct export type ..
  final HSSFCellStyle cellStyle = cell0.getCellStyle();
  final HSSFColor fillBackgroundColorColor = cellStyle.getFillBackgroundColorColor();
  final HSSFColor fillForegroundColorColor = cellStyle.getFillForegroundColorColor();
  assertEquals( "0:0:0", fillBackgroundColorColor.getHexString() );
  assertEquals( "FFFF:FFFF:9999", fillForegroundColorColor.getHexString() );

  // assert that there are no extra columns ..
  final HSSFRow row8 = sheetAt.getRow( 7 );
  assertNull( row8 );

}
 
Example #4
Source File: StyleTest.java    From easyexcel with Apache License 2.0 6 votes vote down vote up
@Test
public void poi() throws Exception {
    InputStream is = new FileInputStream("D:\\test\\styleTest.xls");
    HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
    HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);
    HSSFRow hssfRow = hssfSheet.getRow(0);
    System.out.println(hssfRow.getCell(0).getCellStyle().getDataFormatString());
    DataFormatter formatter = new DataFormatter();
    System.out.println(hssfRow.getCell(0).getNumericCellValue());
    System.out.println(hssfRow.getCell(1).getNumericCellValue());
    System.out.println(hssfRow.getCell(2).getNumericCellValue());
    System.out.println(hssfRow.getCell(0).getCellStyle().getDataFormatString());
    System.out.println(hssfRow.getCell(1).getCellStyle().getDataFormatString());
    System.out.println(hssfRow.getCell(2).getCellStyle().getDataFormatString());

}
 
Example #5
Source File: XlsSessionReader.java    From conference-app with MIT License 6 votes vote down vote up
private List<Session> readAllSessions(InputStream is) {
    final List<Session> result = new ArrayList<Session>();

    try {
        final POIFSFileSystem fileSystem = new POIFSFileSystem(is);
        final HSSFWorkbook workBook = new HSSFWorkbook(fileSystem);
        final HSSFSheet sheet = workBook.getSheet("Alle Tage");

        int rows = sheet.getPhysicalNumberOfRows();
        // as the row is a header we start with the second one
        for (int r = 1; r < rows; r++) {
            final HSSFRow row = sheet.getRow(r);
            if (row == null) {
                continue;
            }
            final Session session = getSessionFromRow(row, r);
            if (session != null) {
                result.add(session);
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Error while reading sessions from file", e);
    }
    return result;
}
 
Example #6
Source File: ExportEventsImpl.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
private void makeHeader ( final List<Field> columns, final HSSFSheet sheet )
{
    final Font font = sheet.getWorkbook ().createFont ();
    font.setFontName ( "Arial" );
    font.setBoldweight ( Font.BOLDWEIGHT_BOLD );
    font.setColor ( HSSFColor.WHITE.index );

    final CellStyle style = sheet.getWorkbook ().createCellStyle ();
    style.setFont ( font );
    style.setFillForegroundColor ( HSSFColor.BLACK.index );
    style.setFillPattern ( PatternFormatting.SOLID_FOREGROUND );

    final HSSFRow row = sheet.createRow ( 0 );

    for ( int i = 0; i < columns.size (); i++ )
    {
        final Field field = columns.get ( i );

        final HSSFCell cell = row.createCell ( i );
        cell.setCellValue ( field.getHeader () );
        cell.setCellStyle ( style );
    }
}
 
Example #7
Source File: DBUnitXLSTestDataCreator.java    From ermasterr with Apache License 2.0 6 votes vote down vote up
@Override
protected void writeDirectTestData(final ERTable table, final Map<NormalColumn, String> data, final String database) {
    final HSSFRow row = sheet.createRow(rowNum++);

    int col = 0;

    for (final NormalColumn column : table.getExpandedColumns()) {
        final HSSFCell cell = row.createCell(col++);

        final String value = Format.null2blank(data.get(column));

        if (value == null || "null".equals(value.toLowerCase())) {

        } else {
            cell.setCellValue(new HSSFRichTextString(value));
        }
    }
}
 
Example #8
Source File: ExportUtil.java    From jumbune with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Adds header to the sheet
 * @param worksheet the worksheet
 * @param sheetName name of sheet
 * @param title title of the sheet (can be null or empty)
 * @param attributes header attributes
 */
public static void addHeader(Worksheet worksheet, String sheetName,
		String title, List<String> attributes) {
	HSSFSheet sheet = worksheet.getSheets().get(sheetName);
	HSSFCellStyle cellStyle = worksheet.getCellStyle();
	HSSFRow row = null;
	int rowNum = 0;
	
	if (title != null && ! title.isEmpty()) {
		row = sheet.createRow(rowNum++);
		row.createCell(0).setCellValue(title);
		char y = (char) ((int) 'A' + attributes.size() - 1);
		String cell = "A1:" + y + "1";
		sheet.addMergedRegion(CellRangeAddress.valueOf(cell));
	}
	
	row = sheet.createRow(rowNum);
	for (int i=0; i<attributes.size(); i++) {
		addCell(row,cellStyle, i, attributes.get(i));
	}
}
 
Example #9
Source File: POIUtils.java    From ermaster-b with Apache License 2.0 6 votes vote down vote up
public static String getCellValue(HSSFSheet sheet, int r, int c) {
	HSSFRow row = sheet.getRow(r);
	
	if (row == null) {
		return null;
	}
	
	HSSFCell cell = row.getCell(c);
	
	if (cell == null) {
		return null;
	}
	
	HSSFRichTextString cellValue = cell.getRichStringCellValue();

	return cellValue.toString();
}
 
Example #10
Source File: AbstractSheetGenerator.java    From ermaster-b with Apache License 2.0 6 votes vote down vote up
protected Map<String, String> buildKeywordsValueMap(HSSFSheet wordsSheet,
		int columnNo, String[] keywords) {
	Map<String, String> keywordsValueMap = new HashMap<String, String>();

	for (String keyword : keywords) {
		CellLocation location = POIUtils.findCell(wordsSheet, keyword,
				columnNo);
		if (location != null) {
			HSSFRow row = wordsSheet.getRow(location.r);

			HSSFCell cell = row.getCell(location.c + 2);
			String value = cell.getRichStringCellValue().getString();

			if (value != null) {
				keywordsValueMap.put(keyword, value);
			}
		}
	}

	return keywordsValueMap;
}
 
Example #11
Source File: PhdIndividualProgramProcessesReport.java    From fenixedu-academic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public HSSFSheet build(final SearchPhdIndividualProgramProcessBean bean) {
    HSSFSheet sheet = workbook.createSheet("Processos de doutoramento");

    setHeaders(sheet);
    List<PhdIndividualProgramProcess> processes =
            PhdIndividualProgramProcess.search(bean.getExecutionYear(), bean.getPredicates());

    int i = 2;
    for (PhdIndividualProgramProcess process : processes) {
        if (!process.isAllowedToManageProcess(Authenticate.getUser())) {
            continue;
        }

        HSSFRow row = sheet.createRow(i);

        fillRow(process, row);
        i++;
    }

    return sheet;
}
 
Example #12
Source File: POIUtils.java    From ermasterr with Apache License 2.0 6 votes vote down vote up
public static List<HSSFCellStyle> copyCellStyle(final HSSFWorkbook workbook, final HSSFRow row) {
    final List<HSSFCellStyle> cellStyleList = new ArrayList<HSSFCellStyle>();

    for (int colNum = row.getFirstCellNum(); colNum <= row.getLastCellNum(); colNum++) {

        final HSSFCell cell = row.getCell(colNum);
        if (cell != null) {
            final HSSFCellStyle style = cell.getCellStyle();
            final HSSFCellStyle newCellStyle = copyCellStyle(workbook, style);
            cellStyleList.add(newCellStyle);
        } else {
            cellStyleList.add(null);
        }
    }

    return cellStyleList;
}
 
Example #13
Source File: POIUtils.java    From ermasterr with Apache License 2.0 6 votes vote down vote up
public static int getIntCellValue(final HSSFSheet sheet, final int r, final int c) {
    final HSSFRow row = sheet.getRow(r);
    if (row == null) {
        return 0;
    }
    final HSSFCell cell = row.getCell(c);

    try {
        if (cell.getCellType() != Cell.CELL_TYPE_NUMERIC) {
            return 0;
        }
    } catch (final RuntimeException e) {
        System.err.println("Exception at sheet name:" + sheet.getSheetName() + ", row:" + (r + 1) + ", col:" + (c + 1));
        throw e;
    }

    return (int) cell.getNumericCellValue();
}
 
Example #14
Source File: DBUnitXLSTestDataCreator.java    From ermaster-b with Apache License 2.0 6 votes vote down vote up
@Override
protected void writeDirectTestData(ERTable table,
		Map<NormalColumn, String> data, String database) {
	HSSFRow row = this.sheet.createRow(this.rowNum++);

	int col = 0;

	for (NormalColumn column : table.getExpandedColumns()) {
		HSSFCell cell = row.createCell(col++);

		String value = Format.null2blank(data.get(column));

		if (value == null || "null".equals(value.toLowerCase())) {

		} else {
			cell.setCellValue(new HSSFRichTextString(value));
		}
	}
}
 
Example #15
Source File: POIUtils.java    From ermasterr with Apache License 2.0 6 votes vote down vote up
public static CellLocation findCell(final HSSFSheet sheet, final String str, final int colNum) {
    for (int rowNum = sheet.getFirstRowNum(); rowNum < sheet.getLastRowNum() + 1; rowNum++) {
        final HSSFRow row = sheet.getRow(rowNum);
        if (row == null) {
            continue;
        }

        final HSSFCell cell = row.getCell(colNum);

        if (cell == null) {
            continue;
        }
        final HSSFRichTextString cellValue = cell.getRichStringCellValue();

        if (!Check.isEmpty(cellValue.getString())) {
            if (cellValue.getString().equals(str)) {
                return new CellLocation(rowNum, (short) colNum);
            }
        }
    }

    return null;
}
 
Example #16
Source File: POIUtils.java    From ermasterr with Apache License 2.0 6 votes vote down vote up
public static CellLocation findMatchCell(final HSSFSheet sheet, final String regexp) {
    for (int rowNum = sheet.getFirstRowNum(); rowNum < sheet.getLastRowNum() + 1; rowNum++) {
        final HSSFRow row = sheet.getRow(rowNum);
        if (row == null) {
            continue;
        }

        final Integer colNum = findMatchColumn(row, regexp);

        if (colNum != null) {
            return new CellLocation(rowNum, colNum.shortValue());
        }
    }

    return null;
}
 
Example #17
Source File: POIUtils.java    From ermasterr with Apache License 2.0 6 votes vote down vote up
public static CellLocation findCell(final HSSFSheet sheet, final String[] strs) {
    for (int rowNum = sheet.getFirstRowNum(); rowNum < sheet.getLastRowNum() + 1; rowNum++) {
        final HSSFRow row = sheet.getRow(rowNum);
        if (row == null) {
            continue;
        }

        for (int i = 0; i < strs.length; i++) {
            final Integer colNum = findColumn(row, strs[i]);

            if (colNum != null) {
                return new CellLocation(rowNum, colNum.shortValue());
            }
        }
    }

    return null;
}
 
Example #18
Source File: ExcelHelp.java    From hy.common.report with Apache License 2.0 6 votes vote down vote up
/**
 * 复制行高
 * 
 * @author      ZhengWei(HY)
 * @createDate  2020-05-29
 * @version     v1.0
 *
 * @param i_FromRow
 * @param io_ToRow
 */
public final static void copyRowHeight(Row i_FromRow ,Row io_ToRow)
{
    if ( i_FromRow instanceof HSSFRow )
    {
        io_ToRow.setHeight(        ((HSSFRow)i_FromRow).getHeight());
        io_ToRow.setHeightInPoints(((HSSFRow)i_FromRow).getHeightInPoints());
        io_ToRow.setZeroHeight(    ((HSSFRow)i_FromRow).getZeroHeight());
    }
    else if ( i_FromRow instanceof SXSSFRow )
    {
        io_ToRow.setHeight(        ((SXSSFRow)i_FromRow).getHeight());
        io_ToRow.setHeightInPoints(((SXSSFRow)i_FromRow).getHeightInPoints());
        io_ToRow.setZeroHeight(    ((SXSSFRow)i_FromRow).getZeroHeight());
    }
    else if ( i_FromRow instanceof XSSFRow )
    {
        io_ToRow.setHeight(        ((XSSFRow)i_FromRow).getHeight());
        io_ToRow.setHeightInPoints(((XSSFRow)i_FromRow).getHeightInPoints());
        io_ToRow.setZeroHeight(    ((XSSFRow)i_FromRow).getZeroHeight());
    }
}
 
Example #19
Source File: ExcelUtil.java    From phone with Apache License 2.0 6 votes vote down vote up
/**
   * 初始化表头
   * @param sheet
   * @param columnJson
   * @param rowNumber
   */
  private static void writeSheetHead(HSSFSheet sheet,JSONObject columnJson,int rowNumber){
if (logger.isDebugEnabled()) {
	logger.debug("writeSheetHead(HSSFSheet, JSONObject, int) - start"); //$NON-NLS-1$
}

Set<String> keySet = columnJson.keySet();
int cellNumber = 0;
HSSFRow row = sheet.createRow(rowNumber);
for (String k : keySet) {//k:GOODS_NO
	String name = columnJson.getString(k);//品项编码
	sheet.autoSizeColumn(cellNumber);
	HSSFCell cell = row.createCell(cellNumber++);
	cell.setCellValue(name);
}

if (logger.isDebugEnabled()) {
	logger.debug("writeSheetHead(HSSFSheet, JSONObject, int) - end"); //$NON-NLS-1$
}
  }
 
Example #20
Source File: Prd5391IT.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testFastExport() throws ResourceException, ReportProcessingException, IOException {
  // This establishes a baseline for the second test using the slow export.

  final MasterReport report = DebugReportRunner.parseLocalReport( "Prd-5391.prpt", Prd5391IT.class );
  final ByteArrayOutputStream bout = new ByteArrayOutputStream();
  FastExcelReportUtil.processXls( report, bout );

  final HSSFWorkbook wb = new HSSFWorkbook( new ByteArrayInputStream( bout.toByteArray() ) );
  final HSSFSheet sheetAt = wb.getSheetAt( 0 );
  final HSSFRow row = sheetAt.getRow( 0 );
  final HSSFCell cell0 = row.getCell( 0 );

  // assert that we are in the correct export type ..
  final HSSFCellStyle cellStyle = cell0.getCellStyle();
  final HSSFColor fillBackgroundColorColor = cellStyle.getFillBackgroundColorColor();
  final HSSFColor fillForegroundColorColor = cellStyle.getFillForegroundColorColor();
  Assert.assertEquals( "0:0:0", fillBackgroundColorColor.getHexString() );
  Assert.assertEquals( "FFFF:8080:8080", fillForegroundColorColor.getHexString() );

  HSSFFont font = cellStyle.getFont( wb );
  Assert.assertEquals( "Times New Roman", font.getFontName() );
}
 
Example #21
Source File: Prd5391IT.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testSlowExport() throws ResourceException, ReportProcessingException, IOException {
  // This establishes a baseline for the second test using the slow export.

  final MasterReport report = DebugReportRunner.parseLocalReport( "Prd-5391.prpt", Prd5391IT.class );
  final ByteArrayOutputStream bout = new ByteArrayOutputStream();
  ExcelReportUtil.createXLS( report, bout );

  final HSSFWorkbook wb = new HSSFWorkbook( new ByteArrayInputStream( bout.toByteArray() ) );
  final HSSFSheet sheetAt = wb.getSheetAt( 0 );
  final HSSFRow row = sheetAt.getRow( 0 );
  final HSSFCell cell0 = row.getCell( 0 );

  // assert that we are in the correct export type ..
  final HSSFCellStyle cellStyle = cell0.getCellStyle();
  final HSSFColor fillBackgroundColorColor = cellStyle.getFillBackgroundColorColor();
  final HSSFColor fillForegroundColorColor = cellStyle.getFillForegroundColorColor();
  Assert.assertEquals( "0:0:0", fillBackgroundColorColor.getHexString() );
  Assert.assertEquals( "FFFF:8080:8080", fillForegroundColorColor.getHexString() );

  HSSFFont font = cellStyle.getFont( wb );
  Assert.assertEquals( "Times New Roman", font.getFontName() );
}
 
Example #22
Source File: ReportStudentsUTLCandidates.java    From fenixedu-academic with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected void getStudentLines(HSSFSheet sheet) {
    int i = 2;
    HSSFRow row;
    while ((row = sheet.getRow(i)) != null) {
        StudentLine studentLine = new StudentLine();
        boolean filledWithSuccess = studentLine.fillWithSpreadsheetRow(forExecutionYear, row);

        try {
            testIt(studentLine);
        } catch (Exception e) {
            filledWithSuccess = false;
        }

        if (filledWithSuccess) {
            correctStudentLines.add(studentLine);
        } else {
            erroneousStudentLines.add(studentLine);
        }

        i++;
    }
}
 
Example #23
Source File: FileUtil.java    From JavaWeb with Apache License 2.0 6 votes vote down vote up
public static void readExcel(String filePth) throws Exception {
	InputStream is = new FileInputStream(filePth);
	//创建工作薄
	//XSSFWorkbook hwb = new XSSFWorkbook(is);
	HSSFWorkbook hwb = new HSSFWorkbook(new POIFSFileSystem(is));
	//得到sheet
	for (int i = 0; i < hwb.getNumberOfSheets(); i++) {
		HSSFSheet sheet = hwb.getSheetAt(i);
		int rows = sheet.getPhysicalNumberOfRows();
		//遍历每一行
		for (int j = 0; j < rows; j++) {
			HSSFRow hr = sheet.getRow(j);
			Iterator<?> it = hr.iterator();
			while(it.hasNext()){
				String context = it.next().toString();
				System.out.println(context);
			}
		}
	}
	hwb.close();
}
 
Example #24
Source File: POIUtils.java    From ermasterr with Apache License 2.0 6 votes vote down vote up
public static void copyRow(final HSSFSheet oldSheet, final HSSFSheet newSheet, final int oldStartRowNum, final int oldEndRowNum, final int newStartRowNum) {
    final HSSFRow oldAboveRow = oldSheet.getRow(oldStartRowNum - 1);

    int newRowNum = newStartRowNum;

    for (int oldRowNum = oldStartRowNum; oldRowNum <= oldEndRowNum; oldRowNum++) {
        POIUtils.copyRow(oldSheet, newSheet, oldRowNum, newRowNum++);
    }

    final HSSFRow newTopRow = newSheet.getRow(newStartRowNum);

    if (oldAboveRow != null) {
        for (int colNum = newTopRow.getFirstCellNum(); colNum <= newTopRow.getLastCellNum(); colNum++) {
            final HSSFCell oldAboveCell = oldAboveRow.getCell(colNum);
            if (oldAboveCell != null) {
                final HSSFCell newTopCell = newTopRow.getCell(colNum);
                newTopCell.getCellStyle().setBorderTop(oldAboveCell.getCellStyle().getBorderBottom());
            }
        }
    }
}
 
Example #25
Source File: XLSPrinter.java    From unitime with Apache License 2.0 5 votes vote down vote up
private ClientAnchorDetail calculateRowLocation(HSSFSheet sheet, int startingRow, double reqImageHeightMM) {
    ClientAnchorDetail clientAnchorDetail;
    HSSFRow row;
    double rowHeightMM = 0.0D;
    double totalRowHeightMM = 0.0D;
    double overlapMM;
    double rowCoordinatesPerMM;
    int toRow = startingRow;
    int inset;

    while (totalRowHeightMM < reqImageHeightMM) {
        row = sheet.getRow(toRow);
        if(row == null) {
            row = sheet.createRow(toRow);
        }
        rowHeightMM = row.getHeightInPoints() / ConvertImageUnits.POINTS_PER_MILLIMETRE;
        totalRowHeightMM += rowHeightMM;
        toRow++;
    }
    toRow--;

    if ((int)totalRowHeightMM == (int)reqImageHeightMM) {
        clientAnchorDetail = new ClientAnchorDetail(startingRow, toRow, ConvertImageUnits.TOTAL_ROW_COORDINATE_POSITIONS);
    } else {
        overlapMM = reqImageHeightMM - (totalRowHeightMM - rowHeightMM);
        if(overlapMM < 0) {
            overlapMM = 0.0D;
        }
        rowCoordinatesPerMM = ConvertImageUnits.TOTAL_ROW_COORDINATE_POSITIONS / rowHeightMM;
        inset = (int)(overlapMM * rowCoordinatesPerMM);
        clientAnchorDetail = new ClientAnchorDetail(startingRow, toRow, inset);
    }
    
    return clientAnchorDetail;
}
 
Example #26
Source File: ExcelExportSuper.java    From phone with Apache License 2.0 5 votes vote down vote up
/**
 * 写入表头
 * @param sheet
 */
void writeHead(HSSFSheet sheet){
	LinkedHashMap<String, Object> columnJson = getColumnJson();
	Set<String> keySet = columnJson.keySet();
	int cellNumber = 0;
	HSSFRow row = sheet.createRow(addRowNumber());
	for (String k : keySet) {
		Object name = columnJson.get(k);//品项编码
		sheet.autoSizeColumn(cellNumber);
		HSSFCell cell = row.createCell(cellNumber++);
		setCellValue(cell, name);
		pubMaxValue(k,name);
	}
}
 
Example #27
Source File: PhdGuidersReport.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void fillParticipant(String guiderRole, final PhdIndividualProgramProcess process, PhdParticipant phdParticipant,
        HSSFRow row) {
    String processNumber = process.getProcessNumber();
    String studentNumber = process.getStudent() != null ? process.getStudent().getNumber().toString() : "";
    String studentName = process.getPerson().getName();

    String participantName = phdParticipant.getName();
    String institution = phdParticipant.getWorkLocation();

    addCellValue(row, onNullEmptyString(processNumber), 0);
    addCellValue(row, onNullEmptyString(studentNumber), 1);
    addCellValue(row, onNullEmptyString(studentName), 2);
    addCellValue(row, onNullEmptyString(participantName), 3);
    addCellValue(row, onNullEmptyString(guiderRole), 4);
    addCellValue(row, onNullEmptyString(institution), 5);

    if (!phdParticipant.isTeacher()) {
        addCellValue(row, onNullEmptyString(null), 6);
        addCellValue(row, onNullEmptyString(null), 7);
        addCellValue(row, onNullEmptyString(null), 8);
    } else {
        InternalPhdParticipant internalPhdParticipant = (InternalPhdParticipant) phdParticipant;
        Teacher teacher = internalPhdParticipant.getTeacher();

        addCellValue(row, onNullEmptyString(teacher.getTeacherId()), 6);
        Department department = internalPhdParticipant.getDepartment();

        addCellValue(row, onNullEmptyString(department != null ? department.getCode() : ""), 7);
        addCellValue(row, onNullEmptyString(department != null ? department.getName() : ""), 8);
    }
}
 
Example #28
Source File: ExcelUtil.java    From jeewx with Apache License 2.0 5 votes vote down vote up
/**
 * 读取 Excel文件内容
 * 
 * @param excel_name
 * @return
 * @throws Exception
 */
public static List<List<Object>> readExcelByList(String excel_name)
		throws Exception {
	// 结果集
	List<List<Object>> list = new ArrayList<List<Object>>();

	HSSFWorkbook hssfworkbook = new HSSFWorkbook(new FileInputStream(
			excel_name));

	// 遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数
	HSSFSheet hssfsheet = hssfworkbook.getSheetAt(0);

	// 遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数
	for (int j = 0; j < hssfsheet.getPhysicalNumberOfRows(); j++) {
		HSSFRow hssfrow = hssfsheet.getRow(j);
		if (hssfrow != null) {
			int col = hssfrow.getPhysicalNumberOfCells();
			// 单行数据
			List<Object> arrayString = new ArrayList<Object>();
			for (int i = 0; i < col; i++) {
				HSSFCell cell = hssfrow.getCell(i);
				if (cell == null) {
					arrayString.add("");
				} else if (cell.getCellType() == 0) {
					arrayString.add(new Double(cell.getNumericCellValue())
							.toString());
				} else {// 如果EXCEL表格中的数据类型为字符串型
					arrayString.add(cell.getStringCellValue().trim());
				}
			}
			list.add(arrayString);
		}
	}
	return list;
}
 
Example #29
Source File: POIUtils.java    From ermaster-b with Apache License 2.0 5 votes vote down vote up
public static String getCellValue(HSSFSheet sheet, CellLocation location) {
	HSSFRow row = sheet.getRow(location.r);
	HSSFCell cell = row.getCell(location.c);

	HSSFRichTextString cellValue = cell.getRichStringCellValue();

	return cellValue.toString();
}
 
Example #30
Source File: ExcelUtil.java    From phone with Apache License 2.0 5 votes vote down vote up
/**
 * 读取excel到JsonArray
 * @param sheet
 * @param map 列对应的column
 * @param rowNumber
 * @param array
 * @return 当前的rowNumber
 */
   private static int readSheetBody(HSSFSheet sheet, Map<Integer, String> map, int rowNumber, JSONArray array) {
	if (logger.isDebugEnabled()) {
		logger.debug("readSheetBody(HSSFSheet, Map<Integer,String>, int, JSONArray) - start"); //$NON-NLS-1$
	}

   	if(sheet!=null){
		int end = sheet.getLastRowNum();//获取最后一行
		for (; rowNumber<=end;rowNumber++) {
			HSSFRow row = sheet.getRow(rowNumber);
			if (row!=null) {
				JSONObject jsonObject = new JSONObject();
				Iterator<Cell> iterator = row.cellIterator();
				while (iterator.hasNext()) {
					Cell cell = iterator.next();
					if (cell!=null) {
						int cellIndex = cell.getColumnIndex();
						String key = map.get(cellIndex);
						String cellValue = getStringValue(cell);
						if (key!=null&&cellValue!=null&&!cellValue.equals("null")) {
							readSheetCell(jsonObject,key,cellValue);
						}
						
					}
				}
				array.add(jsonObject);
			}
		}
	}

	if (logger.isDebugEnabled()) {
		logger.debug("readSheetBody(HSSFSheet, Map<Integer,String>, int, JSONArray) - end"); //$NON-NLS-1$
	}
	return rowNumber;
}