Artikel ini menjelaskan metode Java untuk membaca dan menghasilkan file Excel. Bagikan untuk referensi Anda, sebagai berikut:
1. Baca file Excel
Anda perlu mengunduh poi-3.0.1-final-20070705.jar pertama (klik di sini untuk mengunduh poi-3.0.1-final-20070705.jar .)
Excelexamread.java
impor java.io.file; impor java.io.fileInputStream; impor java.io.ioException; impor org.apache.poi.hssf.usermodel.hssfcell; impor org.apache.poi.hssf.usermodel.hssfrow; impor; org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;public class ExcelExamRead {/** Read Excel file content*/public void showExcel(String excelName) {File file = new File(excelName);FileInputStream in = null;try {// Create Referensi ke file workbook excel di = FileInputStream baru (file); hssfworkbook hwb = hssfworkbook baru (IN); hssfsheet sheet = hwb.getsheet ("myfirstexcel"); referensi lembar kerja di bawah ini. file.getAbsolutePath () + "konten:"); hssfrow row = null; hssfcell cell = null; int rownum = 0; // label baris int colnum = 0; // Label kolom untuk (; rownum <9; rownum ++) {// dapatkan rownum row = sheet.getRow ((pendek) rownum); untuk (colnum = 0; colnum <5; colnum ++) {cell = row.getCell ((pendek) colnum); // Buat objek sel sesuai dengan posisi sistem baris saat ini.out.print (cell.getStringCellValue ()+"/t"); // Dapatkan konten di sel} cell} saat ini. // line break}in.close();} catch (Exception e) {System.out.println("Read Excel file" + file.getAbsolutePath() + "Failed:" + e);} finally {if (in != null) {try {in.close();} catch (IOException e1) {}}}} public static void main(String[] args) {excelexamread excel = excelexamRead (); string excelname = "d: /excelexamread.xls"; excel.showexcel (excelname);}}2. Hasilkan file excel
Excelexamwrite.java:
Impor java.io.file; impor java.io.fileoutputStream; impor java.io.ioException; impor org.apache.poi.hssf.usermodel.hssfcell; import org.apache.poi.hssf.usermodel.hssfcellle; org.apache.poi.hssf.usermodel.hssfsheet; impor org.apache.poi.hssf.usermodel.hssfsheet; impor org.apache.poi.hssf.usermodel.hssfworkbook; impor org.apache.poi.hssf.usmodel.hssfworkbook; impor org.apache.poi.hssf.hsf.hsf.hssfworkbook; Impor org.apache.poi.hssf.hssf.hsf.hsf.hsfworkbook; Impor org.apache.poi.hsels ExcelExamWrite {// Buat file Excel baru, tambahkan 5 baris dan 5 kolom konten, dan tambahkan sel besar yang menggabungkan 2 baris dan 5 kolom dan 5 sel menggabungkan yang menggabungkan 2 baris dan 1 kolom. public void createExcel(String fileName) {File file = new File(fileName);// Create excel file object FileOutputStream fOut = null;try {// Create a new HSSFWorkbook object HSSFWorkbook workbook = new HSSFWorkbook();// Create an Excel worksheet, you can specify the name of the worksheet HSSFSheet sheet = workbook.createSheet ("myfirstexcel"); // Buat font, merah dan tebal hssffont font = workbook.createFont (); font.setColor (hssffont.color_red); font.setboldweight (hssffont.boldweight_bold); hssfont (hssffont.boldweight_bold); hsssfont (hssffont.boldweight_bold); hsssfont (hssffont. non-bold font1.setColor (hssffont.color_normal); font1.setboldweight (hssffont.boldweight_normal); // Buat format sel, seperti centering, alignment kiri, dll. HSSFCELLSTYLE Cellstyle = workbook.createCellstyle (); cellstyle.setalignment (hssfcellstyle.align_center); // Sejajarkan di pusat arah horizontal // Sejajarkan di pusat arah vertikal Cellstyle.setVerticalAlignment (hssfcellstyle.vertical_center); cellstyle.setfont (font); // Atur font hssfcellstyle cellstyle1 = workbook.createCellstyle (); cellstyle1.setalignment (hssfcellstyle.align_left); cellstyle1.setfont (font1); // di bawah ini akan membuat tabel 4 baris dan 3 kolom. Perilaku pertama adalah header. int rownum = 0; // tanda tanda int colnum = 0; // tanda kolom // Buat informasi header hssfrow row = sheet.createrow ((pendek) rownum); // Buat baris pada indeks 0 hssfcell cell = null; // sel untuk (colnum = 0; colnum <5; colnum ++) {// Buat sel pada kolom colnum dari sel baris saat ini = row.createCell ((pendek) colnum); // Tentukan sel sebagai tipe karakter, atau inid. Untuk mendukung bahasa Cina, encoding_utf_16cell.setencoding (hssfcell.encoding_utf_16); cell.setCellstyle (CellStyle); // format cell.setCellValue ("header-thread tabel" + (colnum + 1) + "kolom"); // tambahkan konten ke sel} rownum ++; untuk (; rownum <5; rownum ++) {// Buat baris rownum baru = sheet.createrow ((pendek) rownum); untuk (colnum = 0; colnum <5; colnum ++) {// Buat sel pada posisi colnum dari sel baris saat ini = row.createCell ((pendek) colnum); cell.setencoding (hssfcell.encoding_utf_16); cell.setcellstyle (cellstyle1; "Rowth"+(colnum+1)+"kolom");}} // menggabungkan sel // pertama -tama membuat sel dengan 2 baris dan 5 kolom, dan kemudian menggabungkan sel -sel ini menjadi 2 sel besar rownum = 5; untuk (; rownum <9; rownum ++) {row = sheet.createrow rown (pendek) rownum); untuk (colnum {colnum: colnum; colnum; colnum; colnum (pendek (pendek) rownum); for/ colnum a at the cell = row = sheet.createrow rown (pendek) rownum); for {colnum {colnum; Posisi colnum dari sel baris saat ini = row.createCell ((pendek) colnum);}} // Buat sel besar pertama dengan tinggi 2 dan lebar 2 rownum = 5; colnum = 0; wilayah wilayah = wilayah baru (rownum, sel (pendek) colnum, (rownum + 1), (pendek) (colnum + 4); sel sel) (rownum + 1), (pendek) (colnum + 4); sheet. sheet.getrow (rownum) .getcell ((pendek) colnum); sel.setencoding (hssfcell.encoding_utf_16); sel.setcellstyle (cellstyle); sel.setCellValue ("gabungan sel baris"); // Buat sel besar kedua dengan ketinggian 2 dan lebar 3 ROWNUM = 7 row cell ");// Buat sel besar kedua dengan ketinggian 2 dan lebar 3 ROWNUM = 7 ROWUM; untuk (colnum = 0; colnum <5; colnum ++) {region = region baru (rownum, (pendek) colnum, (rownum+1), (pendek) (colnum)); sheet.addmergedregion (region); // Dapatkan sel sel besar kedua = sheet.getrow (rownum) .gelcell (region); colNum);cell.setEncoding(HSSFCell.ENCODING_UTF_16);cell.setCellStyle(cellStyle);cell.setCellValue("Merge column cells");}// Create a new output file stream fOut = new FileOutputStream(file);// Write the created content to the specified Excel file workbook.write (fout); fout.flush (); fout.close (); // Operasi berakhir, tutup file system.out.println ("Excel Proepsed berhasil!/nThe Path Storage dari file Excel adalah:"+ file.getAbsolutePath ());} Catch (Exception e) {System.out.out. "Buat Gagal/Nalet Alasannya adalah:" + e);} akhirnya {if (fout! = Null) {coba {fout.close ();} catch (ioException e1) {}}}} public static main (string [] args) melempar pengecualian {excelexamwrite new excel = excelex baru (string [] args) {excelexamwrite new excel = excelex baru (string [] args) {excelexamwrite new excel = excelex baru excelex (string [] arg) {excelexamwrite new excel = excelx new excelx need excelx new excelx need excelx new excelex (string []) "E: /ExcelExamwrite.xls"; excel.createExcel (nama file);}}Untuk informasi lebih lanjut tentang konten terkait Java, silakan periksa topik -topik situs ini: "Ringkasan Keterampilan Operasi Java Excel", "Ringkasan Java+MySQL Programming Database", "Tutorial tentang Struktur Data Java dan Algoritma", "Ringkasan File Java dan Keterampilan Operasi Direktori" dan "Ringkasan JAVA DOMASI NOPE NOPE" JAUDE NOPER "
Saya harap artikel ini akan membantu pemrograman Java semua orang.