读写Excel2003文档
生活随笔
收集整理的這篇文章主要介紹了
读写Excel2003文档
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.程序說明1.1編程語言:Java1.2 第三方庫:Apache POIApache POI 官網:http://poi.apache.org/下載頁面:http://poi.apache.org/download.html版本3.8下載地址:http://www.apache.org/dyn/closer.cgi/poi/release/bin/poi-bin-3.8-20120326.zip1.3程序功能使用Apache POI讀寫Microsoft Excel文件1.4程序作者Fans同學2.程序源代碼package excel;import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;/**
* 使用Apache POI讀寫Microsoft Excel
*
* @author Fans.Lei
*
*/
public class ExcelDemo {
/** Excel 文件要存放的位置,假定在c盤poi目錄下 */
public static String filePath = "C:/poi/fans.xls";public static String sheetName = "Fans同學1.0";// 標題欄
private String[] titles = { "姓名", "性別", "班級 ", "專業", " 學歷", " 學校", " 口號"," 備注" };
// 信息欄
private String[][] infos = {
{ "Fans同學", "男", "083", "軟件工程", "本科", "武漢科技大學",
"軟林至尊,Fans同盟。號令天下,莫敢不從。", " 雷文" },
{ "刺客", "男", "083", "軟件工程", "本科", "武漢科技大學",
"圖書館,第二列后七行,司馬非馬,最后的刺客,專諸,絕。", "鄭富強" } };//入口函數public static void main(String args[]) {
ExcelDemo excelDemo = new ExcelDemo();
excelDemo.createExcel();
excelDemo.readExcel();
}// 創建excel文件
public void createExcel() {
try {
// 創建新的Excel 工作簿
HSSFWorkbook workbook = new HSSFWorkbook();// 在Excel工作簿中建一工作表,其名為sheetName
HSSFSheet sheet = workbook.createSheet(sheetName);// 標題欄樣式
HSSFFont font = workbook.createFont();
font.setColor(HSSFFont.COLOR_RED);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyle boldRed = workbook.createCellStyle();
boldRed.setFont(font);// 在索引0的位置創建行(最頂端的行)
HSSFRow titleRow = sheet.createRow(0);
// 向標題欄寫內容
for (int columnIndex = 0; columnIndex < titles.length; columnIndex++) {HSSFCell cell = titleRow.createCell(columnIndex);
cell.setCellValue(titles[columnIndex]);
cell.setCellStyle(boldRed);
}// 向信息欄寫內容
for (int rowIndex = 0; rowIndex < infos.length; rowIndex++) {HSSFRow infoRow = sheet.createRow(rowIndex + 1);for (int colIndex = 0; colIndex < infos[rowIndex].length; colIndex++) {HSSFCell cell = infoRow.createCell(colIndex);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(infos[rowIndex][colIndex]);}
}// 新建一輸出文件流
FileOutputStream fos = new FileOutputStream(filePath);// 把相應的Excel 工作簿存盤
workbook.write(fos);
fos.flush();
// 操作結束,關閉文件
fos.close();
System.out.println(filePath + "已創建!");} catch (Exception e) {
System.out.println(e.getMessage());
}
}// 讀取excel文件
public void readExcel() {
try {
// 創建對Excel工作簿文件的引用
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
filePath));// 創建對工作表的引用。
HSSFSheet sheet = workbook.getSheet(sheetName);Iterator<Row> row = sheet.rowIterator();
while (row.hasNext()) {Row curRow = row.next();
Iterator<Cell> cell = curRow.cellIterator();while (cell.hasNext()) {
String cellValue = cell.next().getStringCellValue();
System.out.print(cellValue + "\t");
}
System.out.println();
}} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}// 讀取excel文件
public void readExcel2() {
try {
// 創建對Excel工作簿文件的引用
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filePath));// 創建對工作表的引用。
HSSFSheet sheet = workbook.getSheet(sheetName);// 在索引0的位置創建行(最頂端的行)
HSSFRow titleRow = sheet.getRow(0);
for (int index = 0; index < titles.length; index++) {// 在索引0的位置創建單元格(左上端)
HSSFCell cell = titleRow.getCell(index);
if (cell != null) {
System.out.println(cell.getStringCellValue());
}
}for (int rowIndex = 0; rowIndex < infos.length; rowIndex++) {HSSFRow infoRow = sheet.getRow(rowIndex + 1);for (int colIndex = 0; colIndex < infos[rowIndex].length; colIndex++) {HSSFCell cell = infoRow.getCell(colIndex);
if (cell != null) {
System.out.println(cell.getStringCellValue());
}
}
}} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}}
3.程序運行結果3.1 控制臺
import java.io.FileOutputStream;
import java.util.Iterator;import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;/**
* 使用Apache POI讀寫Microsoft Excel
*
* @author Fans.Lei
*
*/
public class ExcelDemo {
/** Excel 文件要存放的位置,假定在c盤poi目錄下 */
public static String filePath = "C:/poi/fans.xls";public static String sheetName = "Fans同學1.0";// 標題欄
private String[] titles = { "姓名", "性別", "班級 ", "專業", " 學歷", " 學校", " 口號"," 備注" };
// 信息欄
private String[][] infos = {
{ "Fans同學", "男", "083", "軟件工程", "本科", "武漢科技大學",
"軟林至尊,Fans同盟。號令天下,莫敢不從。", " 雷文" },
{ "刺客", "男", "083", "軟件工程", "本科", "武漢科技大學",
"圖書館,第二列后七行,司馬非馬,最后的刺客,專諸,絕。", "鄭富強" } };//入口函數public static void main(String args[]) {
ExcelDemo excelDemo = new ExcelDemo();
excelDemo.createExcel();
excelDemo.readExcel();
}// 創建excel文件
public void createExcel() {
try {
// 創建新的Excel 工作簿
HSSFWorkbook workbook = new HSSFWorkbook();// 在Excel工作簿中建一工作表,其名為sheetName
HSSFSheet sheet = workbook.createSheet(sheetName);// 標題欄樣式
HSSFFont font = workbook.createFont();
font.setColor(HSSFFont.COLOR_RED);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyle boldRed = workbook.createCellStyle();
boldRed.setFont(font);// 在索引0的位置創建行(最頂端的行)
HSSFRow titleRow = sheet.createRow(0);
// 向標題欄寫內容
for (int columnIndex = 0; columnIndex < titles.length; columnIndex++) {HSSFCell cell = titleRow.createCell(columnIndex);
cell.setCellValue(titles[columnIndex]);
cell.setCellStyle(boldRed);
}// 向信息欄寫內容
for (int rowIndex = 0; rowIndex < infos.length; rowIndex++) {HSSFRow infoRow = sheet.createRow(rowIndex + 1);for (int colIndex = 0; colIndex < infos[rowIndex].length; colIndex++) {HSSFCell cell = infoRow.createCell(colIndex);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(infos[rowIndex][colIndex]);}
}// 新建一輸出文件流
FileOutputStream fos = new FileOutputStream(filePath);// 把相應的Excel 工作簿存盤
workbook.write(fos);
fos.flush();
// 操作結束,關閉文件
fos.close();
System.out.println(filePath + "已創建!");} catch (Exception e) {
System.out.println(e.getMessage());
}
}// 讀取excel文件
public void readExcel() {
try {
// 創建對Excel工作簿文件的引用
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
filePath));// 創建對工作表的引用。
HSSFSheet sheet = workbook.getSheet(sheetName);Iterator<Row> row = sheet.rowIterator();
while (row.hasNext()) {Row curRow = row.next();
Iterator<Cell> cell = curRow.cellIterator();while (cell.hasNext()) {
String cellValue = cell.next().getStringCellValue();
System.out.print(cellValue + "\t");
}
System.out.println();
}} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}// 讀取excel文件
public void readExcel2() {
try {
// 創建對Excel工作簿文件的引用
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filePath));// 創建對工作表的引用。
HSSFSheet sheet = workbook.getSheet(sheetName);// 在索引0的位置創建行(最頂端的行)
HSSFRow titleRow = sheet.getRow(0);
for (int index = 0; index < titles.length; index++) {// 在索引0的位置創建單元格(左上端)
HSSFCell cell = titleRow.getCell(index);
if (cell != null) {
System.out.println(cell.getStringCellValue());
}
}for (int rowIndex = 0; rowIndex < infos.length; rowIndex++) {HSSFRow infoRow = sheet.getRow(rowIndex + 1);for (int colIndex = 0; colIndex < infos[rowIndex].length; colIndex++) {HSSFCell cell = infoRow.getCell(colIndex);
if (cell != null) {
System.out.println(cell.getStringCellValue());
}
}
}} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}}
3.程序運行結果3.1 控制臺
3.2Excel內容
轉載于:https://www.cnblogs.com/qitian1/archive/2012/12/08/6463912.html
總結
以上是生活随笔為你收集整理的读写Excel2003文档的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大型Javascript应用架构的模式(
- 下一篇: System.Runtime.Inter