poiを使ってExcelを操作するいくつか方法
4993 ワード
poiを使ってExcelを操作するいくつか方法に使用されたmaven依存 、Excelテーブルに画像を挿入する . Excelテーブルのあるセルの内容を変更します. Excelテーブルに折れ線図を生成する
org.apache.poi
poi-ooxml
3.15
org.apache.poi
poi
3.15
org.apache.poi
poi-examples
3.15
org.apache.poi
poi-ooxml-schemas
3.15
public static void main(String[] args) {
FileOutputStream fileOut = null;
BufferedImage bufferImg;//
try {
// ByteArrayOutputStream , ByteArray
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
// BufferedImage
bufferImg = ImageIO.read(new File("C:\\Users\\admin\\Desktop\\20180414.jpg"));
//
ImageIO.write(bufferImg, "jpg", byteArrayOut);
//
HSSFWorkbook wb = new HSSFWorkbook();
// sheet
HSSFSheet sheet = wb.createSheet();
// HSSFPatriarch EXCEL
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
/**
* 8
* , left,top,right,bottom
*/
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0,
(short) 10, 1, (short) 20, 8);
//
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut
.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
// excel
fileOut = new FileOutputStream("D:\\123.xls");
// excel
wb.write(fileOut);
} catch (IOException io) {
io.printStackTrace();
System.out.println("io erorr : " + io.getMessage());
} finally {
if (fileOut != null) {
try {
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void updateChart() {
try {
//
String filePath = "D:\\ \\ .xlsx";
FileInputStream inPut = new FileInputStream(filePath);
//
Workbook workBook = new XSSFWorkbook(inPut);
FileOutputStream outPut = new FileOutputStream(filePath);
//
Sheet sheet = workBook.getSheetAt(0);
Row row = sheet.getRow(3);
Cell cell = row.getCell(0);
cell.setCellValue(100);
inPut.close();
workBook.write(outPut);
outPut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void test() throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet();
final int NUM_ROWS = 3;
final int NUM_COLUMNS = 10;
Row row;
Cell cell;
for (int i = 0; i < NUM_ROWS; i++) {
row = sheet.createRow(i);
for (int j = 0; j < NUM_COLUMNS; j++) {
cell = row.createCell(j);
if (i == 0) {
cell.setCellValue(2000 + j);
} else {
cell.setCellValue((int) (Math.random() * 100 + 1));
}
}
}
Drawing drawing = sheet.createDrawingPatriarch();
// ,
ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 2, 5, 10, 15);
Chart chart = drawing.createChart(anchor);
ChartLegend legend = chart.getOrCreateLegend();
//
legend.setPosition(LegendPosition.TOP_RIGHT);
ScatterChartData data = chart.getChartDataFactory().createScatterChartData();
ValueAxis bottomAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.BOTTOM);
ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
bottomAxis.setCrosses(AxisCrosses.AUTO_ZERO);
ChartDataSource xs = DataSources.fromStringCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_COLUMNS - 1));
ChartDataSource ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_COLUMNS - 1));
ChartDataSource ys2 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_COLUMNS - 1));
//
data.addSerie(xs, ys1).setTitle(" (: )");
data.addSerie(xs, ys2).setTitle(" (: )");
chart.plot(data, bottomAxis, leftAxis);
String path = "D:\\ \\CHAT1.xlsx";
FileOutputStream fos = new FileOutputStream(path);
workbook.write(fos);
fos.close();
}