java for row_Java Sheet.rowIterator方法代码示例

import org.apache.poi.ss.usermodel.Sheet; //导入方法依赖的package包/类

private void createTable(Sheet sheet) {

StringBuilder createTableSql = new StringBuilder("CREATE TABLE IF NOT EXISTS ");

createTableSql.append(sheet.getSheetName());

createTableSql.append("(");

Iterator rit = sheet.rowIterator();

Row rowHeader = rit.next();

List columns = new ArrayList<>();

for (int i = 0; i < rowHeader.getPhysicalNumberOfCells(); i++) {

createTableSql.append(rowHeader.getCell(i).getStringCellValue());

if (i == rowHeader.getPhysicalNumberOfCells() - 1) {

createTableSql.append(" TEXT");

} else {

createTableSql.append(" TEXT,");

}

columns.add(rowHeader.getCell(i).getStringCellValue());

}

createTableSql.append(")");

if (dropTable)

database.execSQL("DROP TABLE IF EXISTS " + sheet.getSheetName());

database.execSQL(createTableSql.toString());

for (String column : columns) {

Cursor cursor = database.rawQuery("SELECT * FROM " + sheet.getSheetName(), null); // grab cursor for all data

int deleteStateColumnIndex = cursor.getColumnIndex(column); // see if the column is there

if (deleteStateColumnIndex < 0) {

String type = "TEXT";

// missing_column not there - add it

database.execSQL("ALTER TABLE " + sheet.getSheetName() + " ADD COLUMN " + column + " " + type + " NULL;");

}

}

while (rit.hasNext()) {

Row row = rit.next();

ContentValues values = new ContentValues();

for (int n = 0; n < row.getPhysicalNumberOfCells(); n++) {

if (row.getCell(n).getCellType() == Cell.CELL_TYPE_NUMERIC) {

values.put(columns.get(n), row.getCell(n).getNumericCellValue());

} else {

values.put(columns.get(n), row.getCell(n).getStringCellValue());

}

}

long result = database.insertWithOnConflict(sheet.getSheetName(), null, values, SQLiteDatabase.CONFLICT_IGNORE);

if (result < 0) {

throw new RuntimeException("Insert value failed!");

}

}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
怎样使用这个类里面的方法导出Excel文件public class Xlsx { public static <T> int[] handleBatch(String uri, Function<Row, T> convertFunction, Function<List<T>, Integer> handleFunction, int skipHeader) throws IOException { return handleBatch(uri, convertFunction, handleFunction, skipHeader, 100); } public static <T> int[] handleBatch(String uri, Function<Row, T> convertFunction, Function<List<T>, Integer> handleFunction, int skipHeader, int batchSize) throws IOException { Workbook workbook = WorkbookFactory.create(new File(uri)); Sheet sheet = workbook.getSheetAt(0); Iterator<Row> rowIterator = sheet.rowIterator(); int i = 0; while (i < skipHeader) { if (rowIterator.hasNext()) { rowIterator.next(); } i++; } int count = 0; List<T> batch = new LinkedList<>(); int success = 0; while (rowIterator.hasNext()) { Row row = rowIterator.next(); batch.add(convertFunction.apply(row)); if (batch.size() == batchSize) { success += handleFunction.apply(batch); batch.clear(); } count += 1; } workbook.close(); return new int[]{count, success}; } public static <T> List<T> handleBatch(InputStream inputStream, Function2<Row, Map<String, Integer>, T> convertFunction, int headerRowNum) throws IOException { Workbook workbook = WorkbookFactory.create(inputStream); Sheet sheet = workbook.getSheetAt(0); headerRowNum = Math.max(headerRowNum, 1); Map<String, Integer> headMap = new HashMap<>(64); int i = 0; Iterator<Row> rowIterator = sheet.rowIterator(); while (i < headerRowNum) { Row row = rowIterator.next(); for (Iterator<Cell> it = row.cellIterator(); it.hasNext(); ) { Cell cell = it.next(); headMap.put(cell.getStringCellValue(), cell.getColumnIndex()); } i++; } List<T> batch = new ArrayList<>(); int success = 0; while (rowIterator.hasNext()) { Row row = rowIterator.next(); batch.add(convertFunction.invoke(row, headMap)); } workbook.close(); return batch; } }
最新发布
07-15

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值