在excel中创建新行,然后使用java填充它
我只想让代码检查行是否为空,如果不转到下一行并用给定变量填充它
我在internet上尝试了所有可能的代码,但无法解决此问题,但无法进行检查(//检查空行以填充它)
package birdsystem;
import javax.swing.*;
import java.awt.*;
import static java.awt.Color.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.*;
import org.apache.poi.ss.util.CellReference;
/**
*
* @author ali
*/
public class BirdSystem extends JFrame {
//labels
JLabel kindLabel =new JLabel("نوع الطائر");
JLabel sexLabel =new JLabel("الجنس");
JLabel dateLabel =new JLabel("تاريخ الميلاد ");
JLabel colorLabel =new JLabel("لون الحجل ");
JLabel numberLabel =new JLabel("رقم الحجل ");
JLabel notesLabel =new JLabel("ملاحظات عامه ");
//kindcomboBox1 list
String[] items1 = { "كناري", "طقطق", };
//sexcomboBox2 list
String[] items2 = { "غير معروف", "ذكر", "انثى"};
//inputs
JComboBox kindcomboBox1 = new JComboBox( items1 );
JComboBox sexcomboBox2 = new JComboBox( items2 );
JTextField colorInput = new JTextField();
JTextField numberInput = new JTextField();
JTextField dateInput = new JTextField();
JTextArea notesInput = new JTextArea();
JButton submit = new JButton("اضافة");
//font
Font font1 = new Font("", Font.PLAIN, 25);
BirdSystem(){
super ("اضافة طيور");
setLayout(null);
add(kindLabel);
kindLabel.setBounds(450, 70, 300, 50);
kindLabel.setFont(font1 );
add(kindcomboBox1);
kindcomboBox1.setBounds(450, 120, 100, 50);
kindcomboBox1.setFont(font1 );
add(sexLabel);
sexLabel.setBounds(450, 170, 300, 50);
sexLabel.setFont(font1 );
add(sexcomboBox2);
sexcomboBox2.setBounds(450, 220, 200, 50);
sexcomboBox2.setFont(font1 );
add(dateLabel);
dateLabel.setBounds(450, 280, 300, 50);
dateLabel.setFont(font1 );
add(dateInput);
dateInput.setBounds(420, 330, 200, 30);
dateInput.setFont(font1 );
add(colorLabel);
colorLabel.setBounds(450, 370, 200, 30);
colorLabel.setFont(font1 );
add(colorInput);
colorInput.setBounds(420, 410, 200, 30);
colorInput.setFont(font1 );
add(numberLabel);
numberLabel.setBounds(450, 450, 200, 30);
numberLabel.setFont(font1 );
add(numberInput);
numberInput.setBounds(420, 490, 200, 30);
numberInput.setFont(font1 );
add(notesLabel);
notesLabel.setBounds(450, 520, 200, 30);
notesLabel.setFont(font1 );
add(notesInput);
notesInput.setBounds(420, 550, 400, 200);
notesInput.setFont(font1 );
add(submit);
submit.setBounds(420, 900, 200, 40);
submit.setFont(font1 );
submit.setBackground(white);
//button taking action
submit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
//saving labels in String variabels
String colorvalue =colorLabel.getText();
String hejlnumber =numberLabel.getText();
String birthdate =dateLabel.getText();
String generalnote =notesLabel.getText();
//saving inputs in String variabels
String kindcmbo =(String) kindcomboBox1.getSelectedItem();
String sexcmbo =(String) sexcomboBox2.getSelectedItem();
String birthdayInput =dateInput.getText();
String hejilColorInput =colorInput.getText();
String numbersInput =numberInput.getText();
String noteInput = notesInput.getText();
DataFormatter formatter = new DataFormatter();
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet =workbook.createSheet("birds sheet 1");
//creating 3 rows in the sheet
sheet.createRow(0);
//filling fist excel row with labes
sheet.getRow(0).createCell(0).setCellValue(kindLabel.getText());
sheet.getRow(0).createCell(1).setCellValue(sexLabel.getText());
sheet.getRow(0).createCell(2).setCellValue(dateLabel.getText());
sheet.getRow(0).createCell(3).setCellValue(colorLabel.getText());
sheet.getRow(0).createCell(4).setCellValue(numberLabel.getText());
sheet.getRow(0).createCell(5).setCellValue(notesLabel.getText());
//checking for empty row to fill it
for (int i =0 ; i<100;i++)
{
if ( sheet.getRow(i)==null){
sheet.getRow(i).createCell(i+1).setCellValue(notesLabel.getText());
}
}
//locating where file should be saved
File file = new File("D:/birds.xlsx");
FileOutputStream fos =null;
try {
fos = new FileOutputStream(file);
} catch (FileNotFoundException ex) {
Logger.getLogger(BirdSystem.class.getName()).log(Level.SEVERE, null, ex);
}
try {
workbook.write(fos);
} catch (IOException ex) {
Logger.getLogger(BirdSystem.class.getName()).log(Level.SEVERE, null, ex);
}
try {
workbook.close();
} catch (IOException ex) {
Logger.getLogger(BirdSystem.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Writesheet.xlsx written successfully");
}
});
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws NullPointerException {
BirdSystem frame = new BirdSystem();
frame.setSize(1000,1000);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
frame.getContentPane().setBackground( Color.LIGHT_GRAY );
// TODO code application logic here
}
}
任何建议或文件都是巨大的 帮我个忙,先谢谢大家
共 (0) 个答案