Java Excelテキストボックスの追加、読み取り、削除
JavaプログラムでテキストボックスをExcelに追加する方法について説明します.テキストボックスを追加する場合、テキストを追加したり、テキストの方向を設定したり、テキストの位置合わせをしたり、テキストボックスのサイズ、位置を設定したり、色を塗りつぶしたり、画像を塗りつぶしたり、テキストボックスの回転角度、テキストボックス名、オプションのテキスト、テキストボックスの非表示や表示をしたりすることができます.既存のテキストボックスに対して、テキストボックスのテキストの読み取り、塗りつぶし色、塗りつぶしピクチャ、テキストボックス名、不要なテキストボックスの削除などを実現します.以下では、具体的な実装方法をそれぞれ例示する.ツールの使用 Free Spire.XLS for Java(無料版) Jar取得とインポート:公式サイトでパッケージをダウンロードし、libフォルダの下のjarを解凍してjavaプログラムにインポートできます.以下のインポート効果があります.
Javaコードの例
1.テキスト・ボックスの追加
テキストボックスの追加:
2.テキストボックスの読み込み
テキストボックスの読み込み結果:
3.テキスト・ボックスの削除
テキストボックスの削除効果:
(本文完)
Javaコードの例
1.テキスト・ボックスの追加
import com.spire.xls.*;
import com.spire.xls.core.ITextBox;
import com.spire.xls.core.ITextBoxLinkShape;
import java.awt.*;
public class AddTextBox {
public static void main(String[] args) {
//
Workbook wb = new Workbook();
//
Worksheet sheet = wb.getWorksheets().get(0);
// 1
ITextBox textBox1 = sheet.getTextBoxes().addTextBox(3,3,150,300);// 、
textBox1.setText(" ");//
((ITextBoxLinkShape) textBox1).getFill().setFillType(ShapeFillType.SolidColor);//
((ITextBoxLinkShape) textBox1).getFill().setForeColor(new Color(255,218,155));//
textBox1.setHAlignment(CommentHAlignType.Center);//
textBox1.setVAlignment(CommentVAlignType.Center);
textBox1.setTextRotation(TextRotationType.TopToBottom);//
((ITextBoxLinkShape) textBox1).setVisible(true);//
((ITextBoxLinkShape) textBox1).setName(" 1");//
// 2
ITextBox textBox2 = sheet.getTextBoxes().addTextBox(7,10,120,300);// 、
textBox2.setText(" 2");//
((ITextBoxLinkShape) textBox2).getFill().customPicture("tp.png");//
((ITextBoxLinkShape) textBox2).setRotation(30);// 30
((ITextBoxLinkShape) textBox2).setName(" 2");//
((ITextBoxLinkShape) textBox2).setAlternativeText(" ");//
//
wb.saveToFile("AddTextBox.xlsx",ExcelVersion.Version2013);
wb.dispose();
}
}
テキストボックスの追加:
2.テキストボックスの読み込み
import com.spire.xls.*;
import com.spire.xls.core.spreadsheet.shapes.XlsTextBoxShape;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ReadTextBox {
public static void main(String[] args) throws IOException {
// ,
Workbook wb = new Workbook();
wb.loadFromFile("AddTextBox.xlsx");
//
Worksheet sheet = wb.getWorksheets().get(0);
// ,
XlsTextBoxShape textBoxShape1 = (XlsTextBoxShape) sheet.getTextBoxes().get(0);
String text = textBoxShape1.getText();
Color color = textBoxShape1.getFillColor();
String name = textBoxShape1.getName();
System.out.println(" :"+ text + " :" + color + " :"+ name);
// ,
XlsTextBoxShape textBoxShape2 = (XlsTextBoxShape) sheet.getTextBoxes().get(1);
BufferedImage image = textBoxShape2.getFill().getPicture();
ImageIO.write(image,"png", new File("ExtractedImg.png"));
}
}
テキストボックスの読み込み結果:
3.テキスト・ボックスの削除
import com.spire.xls.*;
import com.spire.xls.core.spreadsheet.shapes.XlsTextBoxShape;
public class RemoveTextBox {
public static void main(String[] args) {
//
Workbook wb = new Workbook();
wb.loadFromFile("AddTextBox.xlsx");
//
Worksheet sheet = wb.getWorksheets().get(0);
// ,
XlsTextBoxShape textBoxShape = (XlsTextBoxShape) sheet.getTextBoxes().get(0);
textBoxShape.remove();
//
wb.saveToFile("RemoveTextBox.xlsx",FileFormat.Version2013);
wb.dispose();
}
}
テキストボックスの削除効果:
(本文完)