質問編集履歴
2
誤字
title
CHANGED
File without changes
|
body
CHANGED
@@ -33,7 +33,7 @@
|
|
33
33
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
34
34
|
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties;
|
35
35
|
|
36
|
-
public class
|
36
|
+
public class POITest {
|
37
37
|
|
38
38
|
public static void main(String[] args) {
|
39
39
|
String exportPath = "exportelsx/TestWorkbook.xlsx";
|
1
掲載ソースの修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -15,7 +15,92 @@
|
|
15
15
|
|
16
16
|
|
17
17
|
###該当のソースコード
|
18
|
+
1つにまとめました
|
18
19
|
```
|
20
|
+
import java.awt.Color;
|
21
|
+
import java.io.FileInputStream;
|
22
|
+
import java.io.FileOutputStream;
|
23
|
+
|
24
|
+
import org.apache.poi.ss.usermodel.Picture;
|
25
|
+
import org.apache.poi.ss.usermodel.Sheet;
|
26
|
+
import org.apache.poi.ss.usermodel.Workbook;
|
27
|
+
import org.apache.poi.util.IOUtils;
|
28
|
+
import org.apache.poi.xssf.usermodel.XSSFChildAnchor;
|
29
|
+
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
|
30
|
+
import org.apache.poi.xssf.usermodel.XSSFDrawing;
|
31
|
+
import org.apache.poi.xssf.usermodel.XSSFShapeGroup;
|
32
|
+
import org.apache.poi.xssf.usermodel.XSSFTextBox;
|
33
|
+
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
34
|
+
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties;
|
35
|
+
|
36
|
+
public class HelloJava_bk {
|
37
|
+
|
38
|
+
public static void main(String[] args) {
|
39
|
+
String exportPath = "exportelsx/TestWorkbook.xlsx";
|
40
|
+
String imagePath = "img/image.jpg";
|
41
|
+
|
42
|
+
// 1.WorkBook、Sheet 作成
|
43
|
+
Workbook testWb = new XSSFWorkbook();
|
44
|
+
Sheet testsheet = testWb.createSheet();
|
45
|
+
|
46
|
+
// 2.グループ作成
|
47
|
+
XSSFDrawing testdraw = (XSSFDrawing) testsheet.createDrawingPatriarch();
|
48
|
+
|
49
|
+
XSSFClientAnchor test_Groupanchor = new XSSFClientAnchor(0,0,0,0,1,1,10,10);
|
50
|
+
XSSFShapeGroup testGroup = testdraw.createGroup(test_Groupanchor);
|
51
|
+
//testGroup.setCoordinates(1, 1, 10, 10);
|
52
|
+
|
53
|
+
// 3.グループに従属するEMU_PER_POINTボックスの作成
|
54
|
+
int pic_x1 = 72*2 *XSSFShape.EMU_PER_POINT;
|
55
|
+
int pic_y1 = 25*2 *XSSFShape.EMU_PER_POINT;
|
56
|
+
int pic_x2 = 72*5 *XSSFShape.EMU_PER_POINT;
|
57
|
+
int pic_y2 = 25*3 *XSSFShape.EMU_PER_POINT;
|
58
|
+
XSSFTextBox test_textbox = testGroup.createTextbox(new XSSFChildAnchor(x1,y1,x2,y2));
|
59
|
+
Color col = Color.LIGHT_GRAY;
|
60
|
+
test_textbox.setFillColor(col.getRed(),col.getGreen(),col.getBlue());
|
61
|
+
test_textbox.setText("テキスト");
|
62
|
+
|
63
|
+
// テキストのフォント設定
|
64
|
+
CTTextCharacterProperties rpr = test_textbox.getCTShape().getTxBody().getPArray(0).getRArray(0).getRPr();
|
65
|
+
rpr.addNewLatin().setTypeface("Trebuchet MS");
|
66
|
+
rpr.setSz(900); // 9 pt
|
67
|
+
col = Color.pink;
|
68
|
+
rpr.addNewSolidFill()
|
69
|
+
.addNewSrgbClr()
|
70
|
+
.setVal(new byte[] { (byte) col.getRed(), (byte) col.getGreen(), (byte) col.getBlue() });
|
71
|
+
|
72
|
+
try{
|
73
|
+
//4.Image追加
|
74
|
+
FileInputStream jpeg = new FileInputStream(imagePath);
|
75
|
+
|
76
|
+
byte[] bytes = IOUtils.toByteArray(jpeg);
|
77
|
+
int picIndex = testWb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
|
78
|
+
jpeg.close();
|
79
|
+
|
80
|
+
XSSFClientAnchor anchor = new XSSFClientAnchor(0,0,0,0,2,3,6,10);
|
81
|
+
|
82
|
+
//picと同じ結果になるようにpic2を設定したい。
|
83
|
+
//Picture pic = drawing.createPicture(anchor, picIndex);
|
84
|
+
Picture pic2 = testGroup.createPicture(anchor, picIndex);
|
85
|
+
|
86
|
+
|
87
|
+
}catch(Exception e){
|
88
|
+
System.out.println("picture Error");
|
89
|
+
}
|
90
|
+
try{
|
91
|
+
FileOutputStream fout = new FileOutputStream(exportPath);
|
92
|
+
testWb.write(fout);
|
93
|
+
fout.close();
|
94
|
+
System.out.println("保存完了");
|
95
|
+
}catch(Exception e){
|
96
|
+
System.out.println("保存失敗");
|
97
|
+
}
|
98
|
+
}
|
99
|
+
}
|
100
|
+
```
|
101
|
+
|
102
|
+
投降時のソース
|
103
|
+
```
|
19
104
|
//group は XSSFShapeGroup型で引き渡されてきます
|
20
105
|
try {
|
21
106
|
byte[] bytes = FileUtils.readFileToByteArray(imageFile);
|
@@ -36,7 +121,7 @@
|
|
36
121
|
|
37
122
|
//① 0x0 、 (0,0)未満に配置
|
38
123
|
group.createPicture(anchor, pictureIdx);
|
39
|
-
|
124
|
+
|
40
125
|
//② 指定箇所に、指定サイズで配置
|
41
126
|
patriarch.createPicture(anchor, pictureIdx);
|
42
127
|
|