質問編集履歴

2

誤字

2016/09/14 03:49

投稿

take2016
take2016

スコア8

test CHANGED
File without changes
test CHANGED
@@ -68,7 +68,7 @@
68
68
 
69
69
 
70
70
 
71
- public class HelloJava_bk {
71
+ public class POITest {
72
72
 
73
73
 
74
74
 

1

掲載ソースの修正

2016/09/14 03:48

投稿

take2016
take2016

スコア8

test CHANGED
File without changes
test CHANGED
@@ -32,6 +32,176 @@
32
32
 
33
33
  ###該当のソースコード
34
34
 
35
+ 1つにまとめました
36
+
37
+ ```
38
+
39
+ import java.awt.Color;
40
+
41
+ import java.io.FileInputStream;
42
+
43
+ import java.io.FileOutputStream;
44
+
45
+
46
+
47
+ import org.apache.poi.ss.usermodel.Picture;
48
+
49
+ import org.apache.poi.ss.usermodel.Sheet;
50
+
51
+ import org.apache.poi.ss.usermodel.Workbook;
52
+
53
+ import org.apache.poi.util.IOUtils;
54
+
55
+ import org.apache.poi.xssf.usermodel.XSSFChildAnchor;
56
+
57
+ import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
58
+
59
+ import org.apache.poi.xssf.usermodel.XSSFDrawing;
60
+
61
+ import org.apache.poi.xssf.usermodel.XSSFShapeGroup;
62
+
63
+ import org.apache.poi.xssf.usermodel.XSSFTextBox;
64
+
65
+ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
66
+
67
+ import org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties;
68
+
69
+
70
+
71
+ public class HelloJava_bk {
72
+
73
+
74
+
75
+ public static void main(String[] args) {
76
+
77
+ String exportPath = "exportelsx/TestWorkbook.xlsx";
78
+
79
+ String imagePath = "img/image.jpg";
80
+
81
+
82
+
83
+ // 1.WorkBook、Sheet 作成
84
+
85
+ Workbook testWb = new XSSFWorkbook();
86
+
87
+ Sheet testsheet = testWb.createSheet();
88
+
89
+
90
+
91
+ // 2.グループ作成
92
+
93
+ XSSFDrawing testdraw = (XSSFDrawing) testsheet.createDrawingPatriarch();
94
+
95
+
96
+
97
+ XSSFClientAnchor test_Groupanchor = new XSSFClientAnchor(0,0,0,0,1,1,10,10);
98
+
99
+ XSSFShapeGroup testGroup = testdraw.createGroup(test_Groupanchor);
100
+
101
+ //testGroup.setCoordinates(1, 1, 10, 10);
102
+
103
+
104
+
105
+ // 3.グループに従属するEMU_PER_POINTボックスの作成
106
+
107
+ int pic_x1 = 72*2 *XSSFShape.EMU_PER_POINT;
108
+
109
+ int pic_y1 = 25*2 *XSSFShape.EMU_PER_POINT;
110
+
111
+ int pic_x2 = 72*5 *XSSFShape.EMU_PER_POINT;
112
+
113
+ int pic_y2 = 25*3 *XSSFShape.EMU_PER_POINT;
114
+
115
+ XSSFTextBox test_textbox = testGroup.createTextbox(new XSSFChildAnchor(x1,y1,x2,y2));
116
+
117
+ Color col = Color.LIGHT_GRAY;
118
+
119
+ test_textbox.setFillColor(col.getRed(),col.getGreen(),col.getBlue());
120
+
121
+ test_textbox.setText("テキスト");
122
+
123
+
124
+
125
+ // テキストのフォント設定
126
+
127
+ CTTextCharacterProperties rpr = test_textbox.getCTShape().getTxBody().getPArray(0).getRArray(0).getRPr();
128
+
129
+ rpr.addNewLatin().setTypeface("Trebuchet MS");
130
+
131
+ rpr.setSz(900); // 9 pt
132
+
133
+ col = Color.pink;
134
+
135
+ rpr.addNewSolidFill()
136
+
137
+ .addNewSrgbClr()
138
+
139
+ .setVal(new byte[] { (byte) col.getRed(), (byte) col.getGreen(), (byte) col.getBlue() });
140
+
141
+
142
+
143
+ try{
144
+
145
+ //4.Image追加
146
+
147
+ FileInputStream jpeg = new FileInputStream(imagePath);
148
+
149
+
150
+
151
+ byte[] bytes = IOUtils.toByteArray(jpeg);
152
+
153
+ int picIndex = testWb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
154
+
155
+ jpeg.close();
156
+
157
+
158
+
159
+ XSSFClientAnchor anchor = new XSSFClientAnchor(0,0,0,0,2,3,6,10);
160
+
161
+
162
+
163
+ //picと同じ結果になるようにpic2を設定したい。
164
+
165
+ //Picture pic = drawing.createPicture(anchor, picIndex);
166
+
167
+ Picture pic2 = testGroup.createPicture(anchor, picIndex);
168
+
169
+
170
+
171
+
172
+
173
+ }catch(Exception e){
174
+
175
+ System.out.println("picture Error");
176
+
177
+ }
178
+
179
+ try{
180
+
181
+ FileOutputStream fout = new FileOutputStream(exportPath);
182
+
183
+ testWb.write(fout);
184
+
185
+ fout.close();
186
+
187
+ System.out.println("保存完了");
188
+
189
+ }catch(Exception e){
190
+
191
+ System.out.println("保存失敗");
192
+
193
+ }
194
+
195
+ }
196
+
197
+ }
198
+
199
+ ```
200
+
201
+
202
+
203
+ 投降時のソース
204
+
35
205
  ```
36
206
 
37
207
  //group は XSSFShapeGroup型で引き渡されてきます
@@ -74,7 +244,7 @@
74
244
 
75
245
  group.createPicture(anchor, pictureIdx);
76
246
 
77
-
247
+
78
248
 
79
249
  //② 指定箇所に、指定サイズで配置
80
250