我正在尝试将单元格从一行复制到下一行。复制注释和cellvalues工作正常。现在,如果我在Excel中设置背景颜色并想要将其复制到新单元格,则它不起作用。复制后没有颜色或背景为黑色。 我试过了:
style2.setFillBackgroundColor(cell_master_alt.getCellStyle().getFillBackgroundColor());
style2.setFillForegroundColor(cell_master_alt.getCellStyle().getFillForegroundColor());
style2.setFillPattern(CellStyle.SOLID_FOREGROUND); or
style2.setFillPattern(cell_master_alt.getCellStyle().getFillPattern());
cell_master_neu.setCellStyle(style2);
几个小时没有任何进展,我查看了我要复制的单元格的前景色和背景色。在那里我找到了RGB值。现在我想制作一个具有该值的新Cellstyle,但这不起作用。 首先我试试这个:
style_new.setFillForegroundColor(new XSSFColor(new java.awt.Color(128,128,128)));
但是有一条错误消息:
The method setFillForegroundColor(short) in the type CellStyle is not applicable for the arguments (XSSFColor)
这有什么问题?
谢谢
答案 0 :(得分:3)
在Apache POI中,单元格样式可以应用于多个单元格,因为Excel以二进制格式存储它们,因此只需将单元格样式从一个单元格应用到另一个单元格而不进行任何复制即可:
public class NWAHome {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public NWAHome(){
prepareGUI();
}
public static void main(String[] args){
NWAHome swingMenuDemo = new NWAHome();
swingMenuDemo.showMenuDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("Produkt anlegen");
mainFrame.setSize(400,400);
headerLabel = new JLabel("",JLabel.CENTER );
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
controlPanel = new JPanel();
mainFrame.getContentPane().setLayout(new FormLayout(new ColumnSpec[] {
ColumnSpec.decode("128px"),
ColumnSpec.decode("128px"),
ColumnSpec.decode("128px"),},
new RowSpec[] {
RowSpec.decode("113px"),
RowSpec.decode("113px"),
RowSpec.decode("113px"),}));
mainFrame.getContentPane().add(headerLabel, "1, 1, fill, fill");
mainFrame.getContentPane().add(controlPanel, "3, 1, 1, 2, fill, fill");
controlPanel.setLayout(new MigLayout("", "[][]", "[][][][][]"));
JButton btnNewButton_1 = new JButton("Projekt anzeigen");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
controlPanel.add(btnNewButton_1, "flowy,cell 1 0");
JButton button_1 = new JButton("Projekt anlegen");
controlPanel.add(button_1, "cell 1 0");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
JButton button = new JButton("Projekt ändern");
controlPanel.add(button, "cell 1 1");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
JButton button_2 = new JButton("Projekt löschen");
controlPanel.add(button_2, "flowy,cell 1 2");
JButton btnNewButton = new JButton("Projekt kopieren");
controlPanel.add(btnNewButton, "cell 1 2");
JButton button_3 = new JButton("Projekt archivieren");
controlPanel.add(button_3, "cell 1 3");
JButton button_4 = new JButton("Projekt importieren");
controlPanel.add(button_4, "cell 1 4");
mainFrame.getContentPane().add(statusLabel, "1, 2, fill, fill");
mainFrame.setVisible(true);
}
private void showMenuDemo(){
//create a menu bar
final JMenuBar menuBar = new JMenuBar();
//create menus
JMenu fileMenu = new JMenu("Projekt");
JMenu editMenu = new JMenu("Produkt");
final JMenu aboutMenu = new JMenu("Kriterien");
final JMenu linkMenu = new JMenu("Bewertung");
//create menu items
JMenuItem newMenuItem = new JMenuItem("Anlegen");
newMenuItem.setMnemonic(KeyEvent.VK_N);
newMenuItem.setActionCommand("New");
JMenuItem openMenuItem = new JMenuItem("Anzeigen");
openMenuItem.setActionCommand("Anzeigen");
JMenuItem saveMenuItem = new JMenuItem("Ändern");
saveMenuItem.setActionCommand("Ändern");
JMenuItem cutMenuItem = new JMenuItem("Löschen");
cutMenuItem.setActionCommand("Löschen");
JMenuItem copyMenuItem = new JMenuItem("Kopieren");
copyMenuItem.setActionCommand("Kopieren");
JMenuItem archivierenMenuItem = new JMenuItem("Archivieren");
archivierenMenuItem.setActionCommand("Archivieren");
JMenuItem importierenMenuItem = new JMenuItem("Importieren");
importierenMenuItem.setActionCommand("importieren");
JMenuItem exitMenuItem = new JMenuItem("Exit");
exitMenuItem.setActionCommand("Exit");
MenuItemListener menuItemListener = new MenuItemListener();
newMenuItem.addActionListener(menuItemListener);
openMenuItem.addActionListener(menuItemListener);
saveMenuItem.addActionListener(menuItemListener);
exitMenuItem.addActionListener(menuItemListener);
cutMenuItem.addActionListener(menuItemListener);
copyMenuItem.addActionListener(menuItemListener);
importierenMenuItem.addActionListener(menuItemListener);
final JCheckBoxMenuItem showWindowMenu = new JCheckBoxMenuItem("Show About", true);
showWindowMenu.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(showWindowMenu.getState()){
menuBar.add(aboutMenu);
}else{
menuBar.remove(aboutMenu);
}
}
});
final JRadioButtonMenuItem showLinksMenu =
new JRadioButtonMenuItem("Show Links", true);
showLinksMenu.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(menuBar.getMenu(3)!= null){
menuBar.remove(linkMenu);
mainFrame.repaint();
}else{
menuBar.add(linkMenu);
mainFrame.repaint();
}
}
});
//add menu items to menus
fileMenu.add(newMenuItem);
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.addSeparator();
fileMenu.add(showWindowMenu);
fileMenu.addSeparator();
fileMenu.add(showLinksMenu);
fileMenu.addSeparator();
fileMenu.add(exitMenuItem);
editMenu.add(cutMenuItem);
editMenu.add(copyMenuItem);
editMenu.add( importierenMenuItem);
//add menu to menubar
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(aboutMenu);
menuBar.add(linkMenu);
//add menubar to the frame
mainFrame.setJMenuBar(menuBar);
mainFrame.setVisible(true);
}
class MenuItemListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
statusLabel.setText(e.getActionCommand()
+ " JMenuItem clicked.");
}
}
}