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());
}
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);
}
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();
}
}
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("회원_등록일");
}
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());
}
Aggregations