【Excel】-Java利用POI输入内容到excel表格中
代码如下
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class XLSXCreate {
public static void main(String[] args) {
try {
//1.创建输入流
FileInputStream fip = new FileInputStream("F:\\ceshi_read.xlsx");
//1.1创建输出流
FileOutputStream fop = new FileOutputStream("F:\\create_t.xlsx");
//2.在输入流中获取工作簿
XSSFWorkbook wb = new XSSFWorkbook(fip);
XSSFWorkbook wb1 = new XSSFWorkbook();
//3.在工作簿中获取目标工作表
Sheet sheet = wb.getSheetAt(0);
Sheet sheet1 = wb1.createSheet("ceshi");
//4.在工作表中获取目标行
Row row = sheet.getRow(0);
Row row1 = sheet1.createRow(0);
//5.在目标行行中获取目标单元格的值
String s1 = row.getCell(0).getStringCellValue();
row1.createCell(0).setCellValue(s1);
String s2 = row.getCell(1).getStringCellValue();
row1.createCell(1).setCellValue(s2);
String s3 = row.getCell(2).getStringCellValue();
row1.createCell(2).setCellValue(s3);
String s4 = row.getCell(3).getStringCellValue();
row1.createCell(3).setCellValue(s4);
String s5 = row.getCell(4).getStringCellValue();
row1.createCell(4).setCellValue(s5);
System.out.println(s1+"\t"+s2+"\t"+s3+"\t"+s4+"\t"+s5);
int n = sheet.getPhysicalNumberOfRows();
for(int i = 1;i<n;i++){
row = sheet.getRow(i);
row1 = sheet1.createRow(i);
Double a1 =row.getCell(0).getNumericCellValue();
row1.createCell(0).setCellValue(a1);
String a2 =row.getCell(1).getStringCellValue();
row1.createCell(1).setCellValue(a2);
Date a3 = row.getCell(2).getDateCellValue();
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD");
String a33 = sdf.format(a3);
row1.createCell(2).setCellValue(a33);
double a4 = row.getCell(3).getNumericCellValue();
row1.createCell(3).setCellValue(a4);
String a5 = row.getCell(4).getStringCellValue();
row1.createCell(4).setCellValue(a5);
System.out.printf("%.0f\t%s\t%s\t%.1f\t%s\n",a1,a2,a33,a4,a5);
}
fip.close();
wb1.write(fop);
fop.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

浙公网安备 33010602011771号