如何高效处理Excel文件.txt文档?
- 内容介绍
- 文章标签
- 相关推荐
本文共计814个文字,预计阅读时间需要4分钟。
javapublic static String getContent01(String filePath) { StringBuilder sb=new StringBuilder(); if (new File(filePath).length()==0) { return null; } boolean isE2007=false; // 判断是否是Excel 2007格式 if (filePath.endsWith(.xlsx)) { isE2007=true; } // 根据是否是Excel 2007格式进行相应的处理 // ...}
public static String getContent01(String filePath) {
StringBuffer sb = new StringBuffer();
if (new File(filePath).length() == 0) {
return null;
}
boolean isE2007 = false; // 判断是否是excel2007格式
if (filePath.endsWith("xlsx")) {
isE2007 = true;
}
try {
InputStream input = new FileInputStream(filePath); // 建立输入流
Workbook wb = null;
int sheets = 0;
// 根据文件格式(2003或者2007)来初始化
if (isE2007) {
wb = new XSSFWorkbook(input);
} else {
wb = new HSSFWorkbook(input);
}
sheets = wb.getNumberOfSheets();
for (int a = 0; a < sheets; a++) {
// 获取Sheet表
Sheet sheet = wb.getSheetAt(a);
// 获得合并单元格加入list中
List
/**
* 合并单元格处理--加入list
*
* @param sheet
* @return
*/
public static List
/**
* 判断单元格是否为合并单元格
*
* @param listCombineCell
* 存放合并单元格的list
* @param cell
* 需要判断的单元格
* @param sheet
* sheet
* @return
*/
public static Boolean isCombineCell(List
/**
* 得到合并单元格首行首列的值,并设置给所有合并单元格
*
* @param listCombineCell
* @param cell
* @param sheet
* @return
*/
public static boolean setCellValue(List
/** * 将单元格格式转换 * * @param cell * @return */ public static String setCellStyle(Cell cell) { String cellValue = ""; if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { DecimalFormat df = new DecimalFormat("#.####"); cellValue = df.format(cell.getNumericCellValue()); cellValue = cellValue.replace(" ", "|").replace("\n", "|").replace("\r", "|").replace("\t", "|"); cellValue = DataImportUtils.qj2bj(cellValue); } else { cell.setCellType(HSSFCell.CELL_TYPE_STRING); String c = cell.toString().replace(" ", "|").replace("\n", "|").replace("\r", "|").replace("\t", "|"); cellValue = DataImportUtils.qj2bj(c); } return cellValue; }
本文共计814个文字,预计阅读时间需要4分钟。
javapublic static String getContent01(String filePath) { StringBuilder sb=new StringBuilder(); if (new File(filePath).length()==0) { return null; } boolean isE2007=false; // 判断是否是Excel 2007格式 if (filePath.endsWith(.xlsx)) { isE2007=true; } // 根据是否是Excel 2007格式进行相应的处理 // ...}
public static String getContent01(String filePath) {
StringBuffer sb = new StringBuffer();
if (new File(filePath).length() == 0) {
return null;
}
boolean isE2007 = false; // 判断是否是excel2007格式
if (filePath.endsWith("xlsx")) {
isE2007 = true;
}
try {
InputStream input = new FileInputStream(filePath); // 建立输入流
Workbook wb = null;
int sheets = 0;
// 根据文件格式(2003或者2007)来初始化
if (isE2007) {
wb = new XSSFWorkbook(input);
} else {
wb = new HSSFWorkbook(input);
}
sheets = wb.getNumberOfSheets();
for (int a = 0; a < sheets; a++) {
// 获取Sheet表
Sheet sheet = wb.getSheetAt(a);
// 获得合并单元格加入list中
List
/**
* 合并单元格处理--加入list
*
* @param sheet
* @return
*/
public static List
/**
* 判断单元格是否为合并单元格
*
* @param listCombineCell
* 存放合并单元格的list
* @param cell
* 需要判断的单元格
* @param sheet
* sheet
* @return
*/
public static Boolean isCombineCell(List
/**
* 得到合并单元格首行首列的值,并设置给所有合并单元格
*
* @param listCombineCell
* @param cell
* @param sheet
* @return
*/
public static boolean setCellValue(List
/** * 将单元格格式转换 * * @param cell * @return */ public static String setCellStyle(Cell cell) { String cellValue = ""; if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { DecimalFormat df = new DecimalFormat("#.####"); cellValue = df.format(cell.getNumericCellValue()); cellValue = cellValue.replace(" ", "|").replace("\n", "|").replace("\r", "|").replace("\t", "|"); cellValue = DataImportUtils.qj2bj(cellValue); } else { cell.setCellType(HSSFCell.CELL_TYPE_STRING); String c = cell.toString().replace(" ", "|").replace("\n", "|").replace("\r", "|").replace("\t", "|"); cellValue = DataImportUtils.qj2bj(c); } return cellValue; }

