Search in sources :

Example 96 with HSSFRow

use of org.apache.poi.hssf.usermodel.HSSFRow in project poi by apache.

the class TestExternalFunction method testInvoke.

/**
	 * Checks that an external function can get invoked from the formula
	 * evaluator.
	 */
public void testInvoke() {
    HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("testNames.xls");
    HSSFSheet sheet = wb.getSheetAt(0);
    /**
		 * register the two test UDFs in a UDF finder, to be passed to the evaluator
		 */
    UDFFinder udff1 = new DefaultUDFFinder(new String[] { "myFunc" }, new FreeRefFunction[] { new MyFunc() });
    UDFFinder udff2 = new DefaultUDFFinder(new String[] { "myFunc2" }, new FreeRefFunction[] { new MyFunc2() });
    UDFFinder udff = new AggregatingUDFFinder(udff1, udff2);
    HSSFRow row = sheet.getRow(0);
    // =myFunc("_")
    HSSFCell myFuncCell = row.getCell(1);
    // =myFunc2("_")
    HSSFCell myFunc2Cell = row.getCell(2);
    HSSFFormulaEvaluator fe = HSSFFormulaEvaluator.create(wb, null, udff);
    assertEquals("_abc", fe.evaluate(myFuncCell).getStringValue());
    assertEquals("_abc2", fe.evaluate(myFunc2Cell).getStringValue());
}
Also used : AggregatingUDFFinder(org.apache.poi.ss.formula.udf.AggregatingUDFFinder) HSSFFormulaEvaluator(org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator) DefaultUDFFinder(org.apache.poi.ss.formula.udf.DefaultUDFFinder) AggregatingUDFFinder(org.apache.poi.ss.formula.udf.AggregatingUDFFinder) UDFFinder(org.apache.poi.ss.formula.udf.UDFFinder) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) DefaultUDFFinder(org.apache.poi.ss.formula.udf.DefaultUDFFinder)

Example 97 with HSSFRow

use of org.apache.poi.hssf.usermodel.HSSFRow in project poi by apache.

the class TestPercentEval method testInSpreadSheet.

public void testInSpreadSheet() {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("Sheet1");
    HSSFRow row = sheet.createRow(0);
    HSSFCell cell = row.createCell(0);
    cell.setCellFormula("B1%");
    row.createCell(1).setCellValue(50.0);
    HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
    CellValue cv;
    try {
        cv = fe.evaluate(cell);
    } catch (RuntimeException e) {
        if (e.getCause() instanceof NullPointerException) {
            throw new AssertionFailedError("Identified bug 44608");
        }
        // else some other unexpected error
        throw e;
    }
    assertEquals(CellType.NUMERIC, cv.getCellTypeEnum());
    assertEquals(0.5, cv.getNumberValue(), 0.0);
}
Also used : HSSFFormulaEvaluator(org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) CellValue(org.apache.poi.ss.usermodel.CellValue) AssertionFailedError(junit.framework.AssertionFailedError) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 98 with HSSFRow

use of org.apache.poi.hssf.usermodel.HSSFRow in project javautils by jiadongpo.

the class Excel2003ParseSample method testPoiExcle2003.

/**
 * @Title: testPoiExcle2003
 * @Description: excel2003的对账解析
 * @param     参数
 * @return void    返回类型
 * @throws
 */
@Test
public void testPoiExcle2003() {
    try {
        // 把一张xls的数据表读到wb里
        List<Map<Integer, String>> list = null;
        BufferedInputStream bis = null;
        String xlsFilePath = "/Users/yp-tc-m-2684/Downloads/20160905142026sale.xls";
        File xlsFile = new File(xlsFilePath);
        bis = new BufferedInputStream(new FileInputStream(xlsFile));
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(bis);
        list = new ArrayList<Map<Integer, String>>();
        // 循环工作表Sheet
        for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
            HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
            if (hssfSheet == null) {
                continue;
            }
            Map<String, Integer> maps = new HashMap<String, Integer>();
            for (int j = 4; j < 5; j++) {
                HSSFRow row = hssfSheet.getRow(j);
                for (int i = 0; i < row.getLastCellNum(); i++) {
                    HSSFCell cell = row.getCell(i);
                    maps.put(cell.getRichStringCellValue().getString(), cell.getColumnIndex());
                }
                System.out.println("获取文件[" + xlsFile.getName() + "]列,字段名列表[" + transMapToString(maps) + "]");
            }
            // 循环行Row
            for (int rowNum = 5; rowNum <= hssfSheet.getLastRowNum() - 7; rowNum++) {
                // 默认从第一行开始解析,只读内容
                HSSFRow hssfRow = hssfSheet.getRow(rowNum);
                if (hssfRow == null) {
                    continue;
                }
                Map<Integer, String> mapRows = new HashMap<Integer, String>();
                // 默认从第一列开始解析
                int i = 1;
                for (Cell cell : hssfRow) {
                    HSSFCell xh = (HSSFCell) cell;
                    mapRows.put(Integer.valueOf(i - 1), getValue(xh).trim());
                    i++;
                }
                // System.out.println("map----size:" + map.size());
                list.add(mapRows);
                System.out.println("获取文件[" + xlsFile.getName() + "]数据,第" + rowNum + "行记录[" + transMapToStringInteger(mapRows) + "]");
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : HashMap(java.util.HashMap) FileNotFoundException(java.io.FileNotFoundException) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) BufferedInputStream(java.io.BufferedInputStream) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) Cell(org.apache.poi.ss.usermodel.Cell) Test(org.junit.Test)

Example 99 with HSSFRow

use of org.apache.poi.hssf.usermodel.HSSFRow in project bitcampSCOpen2017 by ryuyj.

the class MemberView method createColumnLabel.

// 시트의 첫번째 라인(행)의 셀 설정 ( 라벨 설정 )
private void createColumnLabel(HSSFSheet sheet) {
    // 첫번재 행 생성 : sheet.createRow(0) <- 0 첫번째 행을의미
    HSSFRow firstRow = sheet.createRow(0);
    // 만들어진 행에서 첫번재 샐 생성 <- 0 은 첫번째를 의미
    HSSFCell cell = firstRow.createCell(0);
    // 만들어진 샐의 내용 설정
    cell.setCellValue("idx");
    // 두번째 샐 생성 : (1)
    cell = firstRow.createCell(1);
    // 두번째 샐의 내용 설정
    cell.setCellValue("회원_아이디");
    cell = firstRow.createCell(2);
    // 두번째 샐의 내용 설정
    cell.setCellValue("회원_이름");
    cell = firstRow.createCell(3);
    // 두번째 샐의 내용 설정
    cell.setCellValue("회원_등록일");
}
Also used : HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow)

Example 100 with HSSFRow

use of org.apache.poi.hssf.usermodel.HSSFRow in project bitcampSCOpen2017 by ryuyj.

the class MemberView method createMemberRow.

// 원본데이터의 내용들을 각 행에 출력
private void createMemberRow(HSSFSheet sheet, Member2 member, int rowNum) {
    // 매개변수 PageRank rank : 표현할 원본 데이터
    // int rowNum : 몇번째 행을 행성할지에 대한 데이터
    // rowNum 번째 행 생성
    HSSFRow row = sheet.createRow(rowNum);
    // 첫번째 샐 생성
    HSSFCell cell = row.createCell(0);
    // 샐 데이터 설정
    cell.setCellValue(member.getIdx());
    // 두번째 샐 생성
    cell = row.createCell(1);
    // 샐 데이터 설정
    cell.setCellValue(member.getMember_id());
    cell = row.createCell(2);
    cell.setCellValue(member.getMember_name());
    cell = row.createCell(3);
    cell.setCellValue(member.getRegdate());
}
Also used : HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow)

Aggregations

HSSFRow (org.apache.poi.hssf.usermodel.HSSFRow)124 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)98 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)82 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)71 HSSFCellStyle (org.apache.poi.hssf.usermodel.HSSFCellStyle)29 FileOutputStream (java.io.FileOutputStream)24 HSSFRichTextString (org.apache.poi.hssf.usermodel.HSSFRichTextString)24 Test (org.junit.Test)18 IOException (java.io.IOException)16 HSSFFormulaEvaluator (org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator)15 HSSFFont (org.apache.poi.hssf.usermodel.HSSFFont)14 File (java.io.File)13 ArrayList (java.util.ArrayList)12 CellValue (org.apache.poi.ss.usermodel.CellValue)10 OutputStream (java.io.OutputStream)8 HashMap (java.util.HashMap)8 Map (java.util.Map)7 AssertionFailedError (junit.framework.AssertionFailedError)7 FileInputStream (java.io.FileInputStream)6 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)6