forked from github/easyexcel
zhuangjiaju
5 years ago
23 changed files with 342 additions and 33 deletions
@ -0,0 +1,138 @@ |
|||||||
|
package com.alibaba.easyexcel.test.temp; |
||||||
|
|
||||||
|
import java.io.FileInputStream; |
||||||
|
import java.io.InputStream; |
||||||
|
import java.util.Date; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
import org.apache.poi.hssf.usermodel.HSSFDateUtil; |
||||||
|
import org.apache.poi.hssf.usermodel.HSSFRow; |
||||||
|
import org.apache.poi.hssf.usermodel.HSSFSheet; |
||||||
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
||||||
|
import org.apache.poi.ss.usermodel.BuiltinFormats; |
||||||
|
import org.apache.poi.ss.usermodel.Cell; |
||||||
|
import org.apache.poi.ss.usermodel.DataFormatter; |
||||||
|
import org.apache.poi.ss.usermodel.DateUtil; |
||||||
|
import org.apache.poi.ss.usermodel.ExcelStyleDateFormatter; |
||||||
|
import org.apache.poi.ss.usermodel.Row; |
||||||
|
import org.apache.poi.ss.usermodel.Sheet; |
||||||
|
import org.apache.poi.ss.usermodel.Workbook; |
||||||
|
import org.apache.poi.ss.usermodel.WorkbookFactory; |
||||||
|
import org.junit.Ignore; |
||||||
|
import org.junit.Test; |
||||||
|
import org.slf4j.Logger; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
|
||||||
|
import com.alibaba.excel.EasyExcel; |
||||||
|
import com.alibaba.fastjson.JSON; |
||||||
|
|
||||||
|
/** |
||||||
|
* 临时测试 |
||||||
|
* |
||||||
|
* @author Jiaju Zhuang |
||||||
|
**/ |
||||||
|
@Ignore |
||||||
|
public class StyleTest { |
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(StyleTest.class); |
||||||
|
|
||||||
|
@Test |
||||||
|
public void test() { |
||||||
|
List<Object> list = EasyExcel.read("D:\\test\\styleTest.xls").sheet().headRowNumber(0).doReadSync(); |
||||||
|
for (Object data : list) { |
||||||
|
LOGGER.info("返回数据:{}", JSON.toJSONString(data)); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@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()); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void poi07() throws Exception { |
||||||
|
InputStream is = new FileInputStream("D:\\test\\styleTest.xlsx"); |
||||||
|
Workbook workbook = WorkbookFactory.create(is); // 这种方式 Excel 2003/2007/2010 都是可以处理的
|
||||||
|
Sheet sheet = workbook.getSheetAt(0); |
||||||
|
Row hssfRow = sheet.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().getDataFormat()); |
||||||
|
System.out.println(hssfRow.getCell(1).getCellStyle().getDataFormat()); |
||||||
|
System.out.println(hssfRow.getCell(2).getCellStyle().getDataFormat()); |
||||||
|
System.out.println(hssfRow.getCell(3).getCellStyle().getDataFormat()); |
||||||
|
System.out.println(hssfRow.getCell(0).getCellStyle().getDataFormatString()); |
||||||
|
System.out.println(hssfRow.getCell(1).getCellStyle().getDataFormatString()); |
||||||
|
System.out.println(hssfRow.getCell(2).getCellStyle().getDataFormatString()); |
||||||
|
System.out.println(hssfRow.getCell(3).getCellStyle().getDataFormatString()); |
||||||
|
isDate(hssfRow.getCell(0)); |
||||||
|
isDate(hssfRow.getCell(1)); |
||||||
|
isDate(hssfRow.getCell(2)); |
||||||
|
isDate(hssfRow.getCell(3)); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void poi0701() throws Exception { |
||||||
|
InputStream is = new FileInputStream("D:\\test\\f1.xlsx"); |
||||||
|
Workbook workbook = WorkbookFactory.create(is); // 这种方式 Excel 2003/2007/2010 都是可以处理的
|
||||||
|
Sheet sheet = workbook.getSheetAt(0); |
||||||
|
print(sheet.getRow(0).getCell(0)); |
||||||
|
print(sheet.getRow(1).getCell(0)); |
||||||
|
print(sheet.getRow(2).getCell(0)); |
||||||
|
print(sheet.getRow(3).getCell(0)); |
||||||
|
} |
||||||
|
|
||||||
|
private void print(Cell cell) { |
||||||
|
System.out.println( |
||||||
|
DateUtil.isADateFormat(cell.getCellStyle().getDataFormat(), cell.getCellStyle().getDataFormatString())); |
||||||
|
System.out.println(cell.getCellStyle().getDataFormat()); |
||||||
|
System.out.println(cell.getCellStyle().getDataFormatString()); |
||||||
|
DataFormatter f = new DataFormatter(); |
||||||
|
System.out.println(f.formatCellValue(cell)); |
||||||
|
if (cell.getCellStyle().getDataFormatString() != null) { |
||||||
|
|
||||||
|
} |
||||||
|
ExcelStyleDateFormatter ff = new ExcelStyleDateFormatter(cell.getCellStyle().getDataFormatString()); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testFormatter() throws Exception { |
||||||
|
ExcelStyleDateFormatter ff = new ExcelStyleDateFormatter("yyyy年m月d日"); |
||||||
|
|
||||||
|
System.out.println(ff.format(new Date())); |
||||||
|
} |
||||||
|
|
||||||
|
private void isDate(Cell cell) { |
||||||
|
System.out.println( |
||||||
|
DateUtil.isADateFormat(cell.getCellStyle().getDataFormat(), cell.getCellStyle().getDataFormatString())); |
||||||
|
System.out.println(HSSFDateUtil.isCellDateFormatted(cell)); |
||||||
|
DataFormatter f = new DataFormatter(); |
||||||
|
System.out.println(f.formatCellValue(cell)); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testBuiltinFormats() throws Exception { |
||||||
|
System.out.println(BuiltinFormats.getBuiltinFormat(48)); |
||||||
|
System.out.println(BuiltinFormats.getBuiltinFormat(57)); |
||||||
|
System.out.println(BuiltinFormats.getBuiltinFormat(28)); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,53 @@ |
|||||||
|
package com.alibaba.easyexcel.test.temp.poi; |
||||||
|
|
||||||
|
import java.io.File; |
||||||
|
import java.io.IOException; |
||||||
|
|
||||||
|
import org.apache.poi.ss.usermodel.DateUtil; |
||||||
|
import org.apache.poi.xssf.streaming.SXSSFRow; |
||||||
|
import org.apache.poi.xssf.streaming.SXSSFSheet; |
||||||
|
import org.apache.poi.xssf.streaming.SXSSFWorkbook; |
||||||
|
import org.apache.poi.xssf.usermodel.XSSFRow; |
||||||
|
import org.apache.poi.xssf.usermodel.XSSFSheet; |
||||||
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
||||||
|
import org.junit.Ignore; |
||||||
|
import org.junit.Test; |
||||||
|
import org.slf4j.Logger; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
|
||||||
|
import com.alibaba.easyexcel.test.util.TestFileUtil; |
||||||
|
|
||||||
|
/** |
||||||
|
* 测试poi |
||||||
|
* |
||||||
|
* @author Jiaju Zhuang |
||||||
|
**/ |
||||||
|
@Ignore |
||||||
|
public class PoiFormatTest { |
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(PoiFormatTest.class); |
||||||
|
|
||||||
|
@Test |
||||||
|
public void lastRowNum() throws IOException { |
||||||
|
String file = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx"; |
||||||
|
SXSSFWorkbook xssfWorkbook = new SXSSFWorkbook(new XSSFWorkbook(file)); |
||||||
|
SXSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0); |
||||||
|
LOGGER.info("一共行数:{}", xssfSheet.getLastRowNum()); |
||||||
|
SXSSFRow row = xssfSheet.getRow(0); |
||||||
|
LOGGER.info("第一行数据:{}", row); |
||||||
|
xssfSheet.createRow(20); |
||||||
|
LOGGER.info("一共行数:{}", xssfSheet.getLastRowNum()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void lastRowNumXSSF() throws IOException { |
||||||
|
String file = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx"; |
||||||
|
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(file); |
||||||
|
LOGGER.info("一共:{}个sheet", xssfWorkbook.getNumberOfSheets()); |
||||||
|
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0); |
||||||
|
LOGGER.info("一共行数:{}", xssfSheet.getLastRowNum()); |
||||||
|
XSSFRow row = xssfSheet.getRow(0); |
||||||
|
LOGGER.info("第一行数据:{}", row); |
||||||
|
xssfSheet.createRow(20); |
||||||
|
LOGGER.info("一共行数:{}", xssfSheet.getLastRowNum()); |
||||||
|
} |
||||||
|
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue