この記事では、Excelファイルを読み取り、生成するJavaメソッドについて説明します。次のように、参照のために共有してください。
1. Excelファイルを読み取ります
POI-3.0.1-FIN-20070705.JAR最初にダウンロードする必要があります(ここをクリックしてPOI-3.0.1-FIN-20070705.JARをダウンロードしてください。)
excelexamread.java
Import java.io.file; Import java.io.fileinputStream; Import java.io.ioexception; Import org.apache.poi.hssf.usermodel.hssfcell; import org.apache.poi.hssf.usermodel.hssfrow; Import; 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){null; file file = excelname(excelname); Excel Workbookへの参照in = new FileInputStream(file); hssfworkbook hwb = new hssfworkbook(in); hssfsheet sheet = hwb.getsheet( "myfirstexcel"); file.getabsolutepath() + "content:"); hssfrow row = null; hssfcell cell = null; int rownum = 0; // rowラベルint colnum = 0; //(; rownum <9; rownum ++)の列ラベル{// rownum row = sheet.getrow((short)rownum); for(colnum = 0; colnum <5; colnum ++){cell = row.getCell((short)colnum); //現在のrow system.out.print(cell.getStringCellValue()+"/t"); //現在のセルのコンテンツを取得します。 // line break} in.close();} catch(exception e){system.out.println( "read excel file" + file.getabsolutepath() + "faild:" + e);}最後に{if(in!= null){try {in.close();} {excelexamread excel = new excelexamread(); string excelname = "d:/excelexamread.xls"; excel.showexcel(excelname);}}}}2。Excelファイルを生成します
excelexamwrite.java:
Import java.io.file; Import java.io.fileoutputStream; Import java.io.ioexception; import org.apache.poi.hssf.usermodel.hssfcell; import org.apache.poi.hssf.usermodel.hssfcellstyle; Import; org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.hssf.util.Region;//Create Excel file public class excelexamwrite {//新しいExcelファイルを作成し、5行と5列の内容を追加し、2行と5列をマージする5列と5列をマージする大きなセルを追加します。 public void createexcel(string filename){file file = new file(filename); // excel file fileoutputstream fout = null; try {//新しいhssfworkbook workbook = new hssfworkbook(); // excelワークシートを作成することができます。 workbook.createsheet( "myfirstexcel"); //フォント、赤と太字のフォント= workbook.createfont(); font.setcolor(hssffont.color_red); font.setboldweight(hssffont.boldweight_old); hssffont font1 = create font1 non-bold font1.setcolor(hssffont.color_normal); font1.setboldweight(hssffont.boldweight_normal); //センター、左アライメントなどなど、セル形式を作成します。 workbook.createCellStyle(); cellStyle.setAlignment(hssfcellstyle.align_center); //水平方向の中心に並べる//垂直方向センターセンターCellStyle.SetvertAlignment(hssfcellstyle.vertical_center); cellstyle.setfont(font); //フォントhssfcellstyle cellstyle1 = workbook.createcellstyle(); cellstyle1.setalignment(hssfcellstyle.align_left); cellstyle1.setfont(font1); //以下は4 rowsと3カラムのテーブルを作成します。最初の動作はヘッダーです。 int rownum = 0; // rowマークint colnum = 0; //列マーク//ヘッダー情報の作成//インデックス0で行を作成しますhssfcell cell = null; // for for(colnum = 0; colnum <5; colnum ++){//現在のrowセルのcolnum列にセルを作成します= row.createcell((略)colnum);中国語をサポートするために、encoding_utf_16cell.setencoding(hssfcell.encoding_utf_16); cell.setcellstyle(cellstyle); // format cell.setCellValue( "table header-thread" +(colnum + 1) + "column"); // cellにコンテンツを追加} rownum ++; for(; rownum <5; rownum ++){//新しいrownum row = sheet.createrow((short)rownum)を作成します。 for(colnum = 0; colnum <5; colnum ++){//現在の行のcolnum位置でセルを作成します= row.createcell((short)colnum); cell.setencoding(hssfcell.encoding_utf_16); cell.setcellstyle( +(colnum+1)+"column");}} //マージセル//最初に2列と5列のセルを作成し、次にこれらのセルを2つの大きな細胞に統合し、rownum = 5;現在のrow cell = row.createcell((short)colnum);}} //高さ2と幅2 rownum = 5; colnum = 0;領域領域=新しい領域(rownum、(short)colnum、(rotownum + 1)、(short)(short)(colnum + 4)); sheet. addmergedregion(colnum + 4); Sheet.getrow(rownum).getCell((短い)colnum); cell.setencoding(hssfcell.encoding_utf_16); cell.setcellstyle(cellStyle); cell.setCellValue( "Mesge Row Cells"); for(colnum = 0; colnum <5; colnum ++){region = new region(rownum、(short)colnum、(rownum+1)、(short)(colnum)); sheet.addmergedregion(region); // 2番目の大きなセル= sheet.getrow(rownum)。 colnum); cell.setencoding(hssfcell.encoding_utf_16); cell.setCellStyle(cellStyle); cell.setCellValue( "Merge Column Cells");} //新しい出力ファイルストリームfout = new fileoutputStream(ファイル); workbook.write(fout); fout.flush(); fout.close(); //操作の終了、file system.out.println( "excelファイルの作成に正常に!/n excelファイルのストレージパスは次のとおりです。失敗/n理由は次のとおりです。 " + e);}最後に{if(fout!= null){try {fout.close();} catch(ioException e1){}}}} public static void main(string [] args)throws {excelexamwrite excel = new excelexamwrite =; "e:/excelexamwrite.xls"; excel.createexcel(filename);}}Java関連のコンテンツの詳細については、このサイトのトピックをご覧ください:「Java Operation Excelスキルの要約」、「Java+MySQLデータベースプログラミングの概要」、「Javaデータ構造とアルゴリズムに関するチュートリアル」、Javaファイルとディレクトリ運用スキルの要約」、およびJava Operation Dom Nodeスキルの要約」
この記事がみんなのJavaプログラミングに役立つことを願っています。