javafx中excel里图片怎么导出出excel

Excel(5)
ASP.net(80)
.net(107)
数据库内容导出到EXCEL生成报表和图形的方法,一直是很实用的,今天在看到有篇好文介绍了,先做个记录收藏之
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1371968次
积分:36025
积分:36025
排名:第90名
原创:2254篇
评论:114条
(1186)(4)(21)(20)(3)(128)(1)(9)(5)(2)(13)(11)(10)(9)(3)(15)(6)(7)(5)(3)(2)(1)(4)(1)(6)(7)(18)(46)(38)(72)(23)(17)(11)(2)(6)(27)(8)(1)(11)(3)(11)(13)(13)(17)(17)(20)(1)(2)(6)(16)(14)(30)(26)(12)(22)(9)(21)(25)(29)(15)(4)(10)(30)(23)(22)(4)(5)(10)(16)(8)(7)(32)1385人阅读
JavaFX学习之道(10)
This chapter explains how to use the&FileChooser&class to enable users to navigate the file system. The samples provided in this chapter explain how to open one or several files, configure a file
chooser dialog window, and save the application content.
Unlike other user interface component classes, the&FileChooser&class does not belong to thejavafx.scene.controls&package. However,
this class deserves to be mentioned in the JavaFX UI Controls tutorial, because it supports one of the typical GUI application functions: file system navigation.
The&FileChooser&class is located in the&javafx.stage&package along with the other basic root graphical elements, such as&Stage,&Window,
and&Popup. The View Pictures window in&is an example of the file chooser dialog in Windows.
Figure 26-1 Example of a File Chooser Window
Opening Files
A file chooser can be used to invoke an open dialog window for selecting either a single file or multiple files, and to enable a file save dialog window. To display a file chooser, you typically use the&FileChooser&class.&&provides the simplest way to enable a file chooser in your application.
Example 26-1 Showing a File Chooser
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle(&Open Resource File&);
fileChooser.showOpenDialog(stage);
After the code from&&is added to a JavaFX application, the file chooser
dialog window appears immediately when the application starts, as shown in&.
Figure 26-2 Simple File Chooser
Figure 26-3 File Chooser Window in Linux
Figure 26-4 File Chooser Window in Mac OS
Although in the previous example the file chooser appears automatically when the application starts, a more typical approach is to invoke a file chooser by selecting the corresponding menu item or clicking a dedicated button. In
this tutorial, you create an application that enables a user to click a button and open a one or more pictures located in the file system.&shows the code of the FileChooserSample application that implements this task.
Example 26-2 Opening File Chooser for Single and Multiple Selection
import java.awt.D
import java.io.F
import java.io.IOE
import java.util.L
import java.util.logging.L
import java.util.logging.L
import javafx.application.A
import javafx.event.ActionE
import javafx.event.EventH
import javafx.geometry.I
import javafx.scene.S
import javafx.scene.control.B
import javafx.scene.layout.GridP
import javafx.scene.layout.P
import javafx.scene.layout.VB
import javafx.stage.FileC
import javafx.stage.S
public final class FileChooserSample extends Application {
private Desktop desktop = Desktop.getDesktop();
public void start(final Stage stage) {
stage.setTitle(&File Chooser Sample&);
final FileChooser fileChooser = new FileChooser();
final Button openButton = new Button(&Open a Picture...&);
final Button openMultipleButton = new Button(&Open Pictures...&);
openButton.setOnAction(
new EventHandler&ActionEvent&() {
public void handle(final ActionEvent e) {
File file = fileChooser.showOpenDialog(stage);
if (file != null) {
openFile(file);
openMultipleButton.setOnAction(
new EventHandler&ActionEvent&() {
public void handle(final ActionEvent e) {
List&File& list =
fileChooser.showOpenMultipleDialog(stage);
if (list != null) {
for (File file : list) {
openFile(file);
final GridPane inputGridPane = new GridPane();
GridPane.setConstraints(openButton, 0, 0);
GridPane.setConstraints(openMultipleButton, 1, 0);
inputGridPane.setHgap(6);
inputGridPane.setVgap(6);
inputGridPane.getChildren().addAll(openButton, openMultipleButton);
final Pane rootGroup = new VBox(12);
rootGroup.getChildren().addAll(inputGridPane);
rootGroup.setPadding(new Insets(12, 12, 12, 12));
stage.setScene(new Scene(rootGroup));
stage.show();
public static void main(String[] args) {
Application.launch(args);
private void openFile(File file) {
desktop.open(file);
} catch (IOException ex) {
Logger.getLogger(
FileChooserSample.class.getName()).log(
Level.SEVERE, null, ex
In&, the Open a Picture button enables the user to open a file chooser
for a single selection, and the Open Pictures button enables the user to open a file chooser for multiple selections. The&setOnAction&methods for these buttons are almost identical. The only
difference is in the method that is used to invoke a&FileChooser.
The&showOpenDialog&method shows a new file open dialog in which one file can be selected. The method returns the value that specifies the file chosen by the user or&nullif
no selection has been made.
The&showOpenMultipleDialog&method shows a new file open dialog in which multiple files can be selected. The method returns the value that specifies the list of files chosen by the user or&null&if
no selection has been made. The returned list cannot be modified and throws&UnsupportedOperationException&on each modification attempt.
Both methods do not return results until the displayed open dialog window is dismissed (in other words, until a user commits or cancels the selection).
When you compile and run the FileChooserSample application, it produces the window shown in&.
Figure 26-5 FileChooserSample with Two Buttons
When you click either of the buttons, the dialog window shown in&&appears.
The opened file chooser dialog window shows the location that is the default for your operating system.
Figure 26-6 Default File Chooser Window
Users of the FileChooserSample application may navigate to a directory containing pictures and select a picture. After a file is selected, it is opened with the associated application. The example code implements this by using the&open&method
of the&java.awt.Desktop&class:desktop.open(file);.
You can improve the user experience for this application by setting the file chooser directory to the specific directory that contains the pictures.
Configuring a File Chooser
You can configure the file chooser dialog window by setting the&initialDirectory&and&titleproperties
of a&FileChooser&object.&&shows
how to specify the initial directory and a suitable title to preview and open pictures.
Example 26-3 Setting the Initial Directory and Window Title
import java.awt.D
import java.io.F
import java.io.IOE
import java.util.L
import java.util.logging.L
import java.util.logging.L
import javafx.application.A
import javafx.event.ActionE
import javafx.event.EventH
import javafx.geometry.I
import javafx.scene.S
import javafx.scene.control.B
import javafx.scene.layout.GridP
import javafx.scene.layout.P
import javafx.scene.layout.VB
import javafx.stage.FileC
import javafx.stage.S
public final class FileChooserSample extends Application {
private Desktop desktop = Desktop.getDesktop();
public void start(final Stage stage) {
stage.setTitle(&File Chooser Sample&);
final FileChooser fileChooser = new FileChooser();
final Button openButton = new Button(&Open a Picture...&);
final Button openMultipleButton = new Button(&Open Pictures...&);
openButton.setOnAction(
new EventHandler&ActionEvent&() {
public void handle(final ActionEvent e) {
configureFileChooser(fileChooser);
File file = fileChooser.showOpenDialog(stage);
if (file != null) {
openFile(file);
openMultipleButton.setOnAction(
new EventHandler&ActionEvent&() {
public void handle(final ActionEvent e) {
configureFileChooser(fileChooser);
List&File& list =
fileChooser.showOpenMultipleDialog(stage);
if (list != null) {
for (File file : list) {
openFile(file);
final GridPane inputGridPane = new GridPane();
GridPane.setConstraints(openButton, 0, 0);
GridPane.setConstraints(openMultipleButton, 1, 0);
inputGridPane.setHgap(6);
inputGridPane.setVgap(6);
inputGridPane.getChildren().addAll(openButton, openMultipleButton);
final Pane rootGroup = new VBox(12);
rootGroup.getChildren().addAll(inputGridPane);
rootGroup.setPadding(new Insets(12, 12, 12, 12));
stage.setScene(new Scene(rootGroup));
stage.show();
public static void main(String[] args) {
Application.launch(args);
private static void configureFileChooser(final FileChooser fileChooser){
fileChooser.setTitle(&View Pictures&);
fileChooser.setInitialDirectory(
new File(System.getProperty(&user.home&))
private void openFile(File file) {
desktop.open(file);
} catch (IOException ex) {
Logger.getLogger(
FileChooserSample.class.getName()).log(
Level.SEVERE, null, ex
The&configureFileChooser&method sets the View Pictures title and the path to the user home directory with the My Pictures sub-directory. When you compile and run the
FileChooserSample and click one of the buttons, the file chooser shown in&&appears.
Figure 26-7 Opening a Pictures Library
You can also let the users specify the target directory by using the&DirectoryChooser&class. In the code fragment shown in&, the click of the&browseButton&invokes thedirectoryChooser.showDialog&method.
Example 26-4 Use of the DirectoryChooser Class
final Button browseButton = new Button(&...&);
browseButton.setOnAction(
new EventHandler&ActionEvent&() {
public void handle(final ActionEvent e) {
final DirectoryChooser directoryChooser =
new DirectoryChooser();
final File selectedDirectory =
directoryChooser.showDialog(stage);
if (selectedDirectory != null) {
selectedDirectory.getAbsolutePath();
After the selection is performed, you can assign the corresponding value to the file chooser as follows:&fileChooser.setInitialDirectory(selectedDirectory);&.
Setting Extension Filters
As the next configuration option, you can set the extension filter to determine which files to open in a file chooser, as shown in&.
Example 26-5 Setting an Image Type Filter
import java.awt.D
import java.io.F
import java.io.IOE
import java.util.L
import java.util.logging.L
import java.util.logging.L
import javafx.application.A
import javafx.event.ActionE
import javafx.event.EventH
import javafx.geometry.I
import javafx.scene.S
import javafx.scene.control.B
import javafx.scene.layout.GridP
import javafx.scene.layout.P
import javafx.scene.layout.VB
import javafx.stage.FileC
import javafx.stage.S
public final class FileChooserSample extends Application {
private Desktop desktop = Desktop.getDesktop();
public void start(final Stage stage) {
stage.setTitle(&File Chooser Sample&);
final FileChooser fileChooser = new FileChooser();
final Button openButton = new Button(&Open a Picture...&);
final Button openMultipleButton = new Button(&Open Pictures...&);
openButton.setOnAction(
new EventHandler&ActionEvent&() {
public void handle(final ActionEvent e) {
configureFileChooser(fileChooser);
File file = fileChooser.showOpenDialog(stage);
if (file != null) {
openFile(file);
openMultipleButton.setOnAction(
new EventHandler&ActionEvent&() {
public void handle(final ActionEvent e) {
configureFileChooser(fileChooser);
List&File& list =
fileChooser.showOpenMultipleDialog(stage);
if (list != null) {
for (File file : list) {
openFile(file);
final GridPane inputGridPane = new GridPane();
GridPane.setConstraints(openButton, 0, 1);
GridPane.setConstraints(openMultipleButton, 1, 1);
inputGridPane.setHgap(6);
inputGridPane.setVgap(6);
inputGridPane.getChildren().addAll(openButton, openMultipleButton);
final Pane rootGroup = new VBox(12);
rootGroup.getChildren().addAll(inputGridPane);
rootGroup.setPadding(new Insets(12, 12, 12, 12));
stage.setScene(new Scene(rootGroup));
stage.show();
public static void main(String[] args) {
Application.launch(args);
private static void configureFileChooser(
final FileChooser fileChooser) {
fileChooser.setTitle(&View Pictures&);
fileChooser.setInitialDirectory(
new File(System.getProperty(&user.home&))
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter(&All Images&, &*.*&),
new FileChooser.ExtensionFilter(&JPG&, &*.jpg&),
new FileChooser.ExtensionFilter(&PNG&, &*.png&)
private void openFile(File file) {
desktop.open(file);
} catch (IOException ex) {
Logger.getLogger(FileChooserSample.class.getName()).log(
Level.SEVERE, null, ex
In&, you set an extension filter by using&FileChooser.ExtensionFilter&to
define the following options for file selection: All images, JPG, and PNG.
When you compile, run the FileChooserSample code from&, and click
one of the buttons, the extension filters appear in the file chooser window. If a user selects JPG, then the file chooser displays only pictures of the JPG type.&&captures the moment of selection JPG images in the My Pictures directory.
Figure 26-8 Filtering JPG Files in File Chooser
Saving Files
In addition to opening and filtering files, the&FileChooser&API provides a capability to let a user specify a file name (and its location in the file system) for a
file to be saved by the application. The&showSaveDialog&method of the&FileChooser&class opens a save dialog window. Like other
show dialog methods, the&showSaveDialog&method returns the file chosen by the user or&null&if no selection has been performed.
The code fragment shown in&&is an addition to the&&sample.
It implements one more item of the context menu that saves the displayed image in the file system.
Example 26-6 Saving an Image with the FileChooser Class
MenuItem cmItem2 = new MenuItem(&Save Image&);
cmItem2.setOnAction(new EventHandler&ActionEvent&() {
public void handle(ActionEvent e) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle(&Save Image&);
System.out.println(pic.getId());
File file = fileChooser.showSaveDialog(stage);
if (file != null) {
ImageIO.write(SwingFXUtils.fromFXImage(pic.getImage(),
null), &png&, file);
} catch (IOException ex) {
System.out.println(ex.getMessage());
When you add&&to the MenuSample application (find the source code
in the Application Files), compile and run it, you enable the Save Image menu item, as shown in.
Figure 26-9 Saving Image
After a user selects the Save Image item, the Save Image window shown in&appears.
Figure 26-10 The Save Image Window
The Save Image window corresponds to the typical user experience for the save dialog windows: the user needs to select the target directory, type in the name of the saving file, and click Save.
Related API Documentation&
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:137984次
积分:2837
积分:2837
排名:第9656名
原创:149篇
转载:25篇
(1)(14)(34)(2)(8)(2)(10)(20)(62)(21)Java学习之道(38)
POI学习之道(2)
采用Spring mvc架构:&
Controller层代码如下&
Service层代码如下:&
前台的js代码如下:&
[javascript]&
设置Excel样式以及注意点:
注意点1:合并单元格&&& new CellRangeAddress(int,int,int,int)
first row (0-based) ,last row (0-based), first column (0-based),last column (0-based)
注意点2:合并单元格
String[] excelHeader = { &所属区域(地市)&, &机房&, &机架资源情况&, &&, &&, &&, &&,&&, &端口资源情况&, &&, &&, &&, &&, &&, &机位资源情况&, &&, &&, &设备资源情况&,&&, &&, &IP资源情况&, &&, &&, &&, &&, &网络设备数& };
合并以后的单元格虽然是一个,但是仍然要保留其单元格内容,此处用空字符串代替,否则后续表头显示不出
注意点3:填充单元格
正确写法:
HSSFCell cell = row.createCell(i);
cell.setCellValue(excelHeader1[i]);
cell.setCellStyle(style);
错误写法:
row.createCell(i).setCellValue(excelHeader1[i]);
row.createCell(i).setCellStyle(style);
本人为了省一个HSSFCell对象,使用了错误写法,导致HSSFCell对象创建了2次,最后只保留了样式,而内容无法显示
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:137984次
积分:2837
积分:2837
排名:第9656名
原创:149篇
转载:25篇
(1)(14)(34)(2)(8)(2)(10)(20)(62)(21)您当前所在位置: >
> 编辑语言
JavaFX深入浅出类和对象的技巧
大家知道JavaFX深入浅出类和对象吗?下面我们就给大家详细介绍一下吧!我们积累了一些经验,在此拿出来与大家分享下,请大家互相指正。
一、类的定义
1、有了变量和函数,把它们和到一块不就成了类吗?
class People { var name: S var age: I }class Hello { var people: P function Greeting() { println(&Hello {people.name} ,You are {people.age} years old.&); } }
二、抽象类和接口
1、抽象类用 abstract 关键字修饰
abstract class People { var name: S var age: I }
和其他面向对象的语言一样,抽象类是不能创建实例的。
2、JavaFX 中没有接口 ,但是可以直接使用 Java 中的接口
interface IHello{void Greeting();}
二、类的继承
1、用 extends 关键字从另外一个类继承
class Man extends People{var sex:B}
2、从抽象类或者接口继承,也用 extends 关键字,接口中的方法必须重载,用 override 关键字
class Hello extends IHello{var people: Poverride function Greeting():Void {println(&Hello {people.name} ,You are {people.age} years old.&);}}
3、JavaFX 可以多重继承,即继承多个类和多个接口
三、对象的创建
JavaFX 一般用字面量方式创建对象,也可以用 new
var p = new People();
var p = People{
name:&小明&
四、对象的初始化
1、JavaFX 的类没有构造函数,但是有一个 init 初始化块,当对象创建以后会运行这个块里的代码,例如
class People {init {name=&Jone&;println(&Hello {name} ,You are {age} years old.&);}var name: Svar age: I}
var p =new People();
输出:Hello Jone ,You are 0 years old.
2、还有一个 postinit 块,在所有初始化工作完成以后执行
3、isInitialized 函数,判断一个成员是否被初始化了,例如
class People {var name: Svar age: I init {//检查 name 是否被初始化
if (isInitialized(name)){println(&Hello {name}.&);}else {println(&What&s your name?&);}}}
//创建对象的时候初始化 name
var p = People{name: &Jone&}
输出:Hello Jone.
//创建对象的时候没有初始化 namevar p = People{age: 18}输出:What&s your name?
和 Java 的用法完全一样
相信大家已经学会JavaFX深入浅出类和对象了吧!感谢大家对我们网站的支持!
相关推荐:
上一篇:下一篇:
本文相关阅读
高校查询分数线
考生所在地
北京天津辽宁吉林黑龙江上海江苏浙江安徽福建山东湖北湖南广东重庆四川陕西甘肃河北山西内蒙古河南海南广西贵州云南西藏青海宁夏新疆江西香港澳门台湾
<option value="
<option value="
<option value="
<option value="
<option value="
<option value="
<option value="
<option value="
<option value="
<option value="
<option value="
地区批次线查询
考生所在地
北京天津辽宁吉林黑龙江上海江苏浙江安徽福建山东湖北湖南广东重庆四川陕西甘肃河北山西内蒙古河南海南广西贵州云南西藏青海宁夏新疆江西香港澳门台湾
科目理科文科综合其他
<option value="
<option value="
<option value="
<option value="
<option value="
<option value="
<option value="
<option value="
<option value="
<option value="
<option value="
院校所在地北京天津辽宁吉林黑龙江上海江苏浙江安徽福建山东湖北湖南广东重庆四川陕西甘肃河北山西内蒙古河南海南广西贵州云南西藏青海宁夏新疆江西香港澳门台湾
其它中央部委
类型工科农业师范民族
层次本科高职(专科)
高考志愿③部曲
频道热门推荐
栏目最新更新

我要回帖

更多关于 excel怎么导出表格 的文章

 

随机推荐