mirror of https://github.com/alibaba/easyexcel
zhuangjiaju
5 years ago
26 changed files with 863 additions and 411 deletions
After Width: | Height: | Size: 10 KiB |
@ -0,0 +1,54 @@
|
||||
package com.alibaba.easyexcel.test.demo.poi; |
||||
|
||||
import java.io.File; |
||||
import java.io.IOException; |
||||
|
||||
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 PoiTest { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(PoiTest.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()); |
||||
|
||||
|
||||
} |
||||
} |
@ -0,0 +1,22 @@
|
||||
package com.alibaba.easyexcel.test.demo.write; |
||||
|
||||
import java.util.Date; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* 复杂头数据.这里最终效果是第一行就一个主标题,第二行分类 |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
@Data |
||||
public class ComplexHeadData { |
||||
@ExcelProperty({"主标题", "字符串标题"}) |
||||
private String string; |
||||
@ExcelProperty({"主标题", "日期标题"}) |
||||
private Date date; |
||||
@ExcelProperty({"主标题", "数字标题"}) |
||||
private Double doubleData; |
||||
} |
@ -0,0 +1,35 @@
|
||||
package com.alibaba.easyexcel.test.demo.write; |
||||
|
||||
import java.util.Date; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
import com.alibaba.excel.annotation.format.DateTimeFormat; |
||||
import com.alibaba.excel.annotation.format.NumberFormat; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* 基础数据类.这里的排序和excel里面的排序一致 |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
@Data |
||||
public class ConverterData { |
||||
/** |
||||
* 我想所有的 字符串起前面加上"自定义:"三个字 |
||||
*/ |
||||
@ExcelProperty(value = "字符串标题", converter = CustomStringStringConverter.class) |
||||
private String string; |
||||
/** |
||||
* 我想写到excel 用年月日的格式 |
||||
*/ |
||||
@DateTimeFormat("yyyy年MM月dd日HH时mm分ss秒") |
||||
@ExcelProperty("日期标题") |
||||
private Date date; |
||||
/** |
||||
* 我想写到excel 用百分比表示 |
||||
*/ |
||||
@NumberFormat("#.##%") |
||||
@ExcelProperty(value = "数字标题") |
||||
private Double doubleData; |
||||
} |
@ -0,0 +1,59 @@
|
||||
package com.alibaba.easyexcel.test.demo.write; |
||||
|
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
import com.alibaba.excel.metadata.GlobalConfiguration; |
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty; |
||||
|
||||
/** |
||||
* String and string converter |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class CustomStringStringConverter implements Converter<String> { |
||||
@Override |
||||
public Class supportJavaTypeKey() { |
||||
return String.class; |
||||
} |
||||
|
||||
@Override |
||||
public CellDataTypeEnum supportExcelTypeKey() { |
||||
return CellDataTypeEnum.STRING; |
||||
} |
||||
|
||||
/** |
||||
* 这里是读的时候会调用 不用管 |
||||
* |
||||
* @param cellData |
||||
* NotNull |
||||
* @param contentProperty |
||||
* Nullable |
||||
* @param globalConfiguration |
||||
* NotNull |
||||
* @return |
||||
*/ |
||||
@Override |
||||
public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, |
||||
GlobalConfiguration globalConfiguration) { |
||||
return cellData.getStringValue(); |
||||
} |
||||
|
||||
/** |
||||
* 这里是写的时候会调用 不用管 |
||||
* |
||||
* @param value |
||||
* NotNull |
||||
* @param contentProperty |
||||
* Nullable |
||||
* @param globalConfiguration |
||||
* NotNull |
||||
* @return |
||||
*/ |
||||
@Override |
||||
public CellData convertToExcelData(String value, ExcelContentProperty contentProperty, |
||||
GlobalConfiguration globalConfiguration) { |
||||
return new CellData("自定义:" + value); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,25 @@
|
||||
package com.alibaba.easyexcel.test.demo.write; |
||||
|
||||
import java.util.Date; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* 基础数据类 |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
@Data |
||||
public class IndexData { |
||||
@ExcelProperty(value = "字符串标题", index = 0) |
||||
private String string; |
||||
@ExcelProperty(value = "日期标题", index = 1) |
||||
private Date date; |
||||
/** |
||||
* 这里设置3 会导致第二列空的 |
||||
*/ |
||||
@ExcelProperty(value = "数字标题", index = 3) |
||||
private Double doubleData; |
||||
} |
@ -0,0 +1,32 @@
|
||||
package com.alibaba.easyexcel.test.demo.write; |
||||
|
||||
import java.util.Date; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
import com.alibaba.excel.annotation.write.style.ColumnWidth; |
||||
import com.alibaba.excel.annotation.write.style.ContentRowHeight; |
||||
import com.alibaba.excel.annotation.write.style.HeadRowHeight; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* 基础数据类 |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
@Data |
||||
@ContentRowHeight(10) |
||||
@HeadRowHeight(20) |
||||
@ColumnWidth(25) |
||||
public class WidthAndHeightData { |
||||
@ExcelProperty("字符串标题") |
||||
private String string; |
||||
@ExcelProperty("日期标题") |
||||
private Date date; |
||||
/** |
||||
* 宽度为50 |
||||
*/ |
||||
@ColumnWidth(50) |
||||
@ExcelProperty("数字标题") |
||||
private Double doubleData; |
||||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue