日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

读写Excel2003文档

發(fā)布時間:2024/7/19 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 读写Excel2003文档 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.程序說明1.1編程語言:Java1.2 第三方庫:Apache POIApache POI 官網(wǎng):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同學(xué)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同學(xué)1.0";// 標(biāo)題欄
private String[] titles = { "姓名", "性別", "班級 ", "專業(yè)", " 學(xué)歷", " 學(xué)校", " 口號"," 備注" };
// 信息欄
private String[][] infos = {
{ "Fans同學(xué)", "男", "083", "軟件工程", "本科", "武漢科技大學(xué)",
"軟林至尊,Fans同盟。號令天下,莫敢不從。", " 雷文" },
{ "刺客", "男", "083", "軟件工程", "本科", "武漢科技大學(xué)",
"圖書館,第二列后七行,司馬非馬,最后的刺客,專諸,絕。", "鄭富強(qiáng)" } };
//入口函數(shù)public static void main(String args[]) {
ExcelDemo excelDemo = new ExcelDemo();
excelDemo.createExcel();
excelDemo.readExcel();
}
// 創(chuàng)建excel文件
public void createExcel() {
try {
// 創(chuàng)建新的Excel 工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
// 在Excel工作簿中建一工作表,其名為sheetName
HSSFSheet sheet = workbook.createSheet(sheetName);
// 標(biāo)題欄樣式
HSSFFont font = workbook.createFont();
font.setColor(HSSFFont.COLOR_RED);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyle boldRed = workbook.createCellStyle();
boldRed.setFont(font);
// 在索引0的位置創(chuàng)建行(最頂端的行)
HSSFRow titleRow = sheet.createRow(0);
// 向標(biāo)題欄寫內(nèi)容
for (int columnIndex = 0; columnIndex < titles.length; columnIndex++) {
HSSFCell cell = titleRow.createCell(columnIndex);
cell.setCellValue(titles[columnIndex]);
cell.setCellStyle(boldRed);
}
// 向信息欄寫內(nèi)容
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);
// 把相應(yīng)的Excel 工作簿存盤
workbook.write(fos);
fos.flush();
// 操作結(jié)束,關(guān)閉文件
fos.close();
System.out.println(filePath + "已創(chuàng)建!");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
// 讀取excel文件
public void readExcel() {
try {
// 創(chuàng)建對Excel工作簿文件的引用
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
filePath));
// 創(chuàng)建對工作表的引用。
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 {
// 創(chuàng)建對Excel工作簿文件的引用
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filePath));
// 創(chuàng)建對工作表的引用。
HSSFSheet sheet = workbook.getSheet(sheetName);
// 在索引0的位置創(chuàng)建行(最頂端的行)
HSSFRow titleRow = sheet.getRow(0);
for (int index = 0; index < titles.length; index++) {
// 在索引0的位置創(chuàng)建單元格(左上端)
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.程序運(yùn)行結(jié)果3.1 控制臺

3.2Excel內(nèi)容

轉(zhuǎn)載于:https://www.cnblogs.com/qitian1/archive/2012/12/08/6463912.html

總結(jié)

以上是生活随笔為你收集整理的读写Excel2003文档的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。