java 는 poi. 3.10 을 사용 하여 엑셀 2003 (xls 형식) 을 읽 습 니 다.
8915 단어 Excel
1. 최신 poi 다운로드
http://poi.apache.org/download.html
2. 압축 해제 관련 jar 패키지 도입 프로젝트
3. 사례 소스 코드
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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;
import org.junit.Test;
public class TestExcel {
//
static Log log = LogFactory.getLog(TestExcel.class);
// Excel
public static String filePath = TestExcel.class.getResource("/test.xls").getPath();
@Test
public static void main(String[] args) {
try {
// Excel
HSSFWorkbook wookbook = new HSSFWorkbook(new FileInputStream(
filePath));
// Excel , 0
// :HSSFSheet sheet = workbook.getSheetAt(0);
HSSFSheet sheet = wookbook.getSheet("Sheet1");
// Excel
int rows = sheet.getPhysicalNumberOfRows();
// , 0 , 0 , 。
for (int i = 1; i < rows; i++) {
//
HSSFRow row = sheet.getRow(i);
//
if (row != null) {
// Excel
// int cells = row.getPhysicalNumberOfCells();
String value = "";
//
// POI excel cell.getNumbericCellValue()
// 。
DecimalFormat df = new DecimalFormat("0");
// *** 3 cells , , ***
for (int j = 0; j < 5; j++) {
//
HSSFCell cell = row.getCell(j);
if (cell != null) {
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA:
break;
case HSSFCell.CELL_TYPE_NUMERIC:
value += df.format(cell.getNumericCellValue())
+ ",";
break;
case HSSFCell.CELL_TYPE_STRING:
value += cell.getStringCellValue() + ",";
break;
default:
value += "#"+",";
break;
}
} else {
value += "#" + ",";
}
}
// mysql
String[] val = value.split(",");
System.out.println(val[0]);
System.out.println(val[1]);
System.out.println(val[2]);
System.out.println(val[3]);
System.out.println(val[4]);
System.out.println(" ");
// ******* ******
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
이 방법 은 엑셀 2003 버 전 만 읽 을 수 있 고 2010 버 전 은 본인 이 공부 하고 있 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Excel Grep toolExcel Grep tool ■히나가타 ■ 시트 구성 ExcelGrep.cls...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.