이용하다jar excel 읽기, 수정 및 삭제

1、
현재 POI의 최신 릴리즈는 3.10_FINAL.이 버전에서 보호되는jar 패키지는 다음과 같습니다.
Maven artifactId
Prerequisites
JAR
poi
commons-logging, commons-codec, log4j
poi-version-yyyymmdd.jar
poi-scratchpad
poi
poi-scratchpad-version-yyyymmdd.jar
poi-ooxml
poi, poi-ooxml-schemas
poi-ooxml-version-yyyymmdd.jar
poi-ooxml-schemas
xmlbeans
poi-ooxml-schemas-version-yyyymmdd.jar
poi-examples
poi, poi-scratchpad, poi-ooxml
poi-examples-version-yyyymmdd.jar
ooxml-schemas
xmlbeans
ooxml-schemas-1.1.jar
HSSF - Microsoft Excel XLS 형식 파일을 읽고 쓸 수 있는 기능을 제공합니다.XSSF - Microsoft Excel OOXML XLSX 형식 파일을 읽고 쓰는 기능을 제공합니다.HWPF - Microsoft Word DOC 형식 파일을 읽고 쓸 수 있습니다.HSLF - Microsoft PowerPoint 형식의 파일을 읽고 쓸 수 있는 기능을 제공합니다.HDGF - Microsoft Visio 형식 파일을 읽는 기능을 제공합니다.HPBF - Microsoft Publisher 형식의 파일을 읽는 기능을 제공합니다.HSMF - Microsoft Outlook 형식 파일을 읽는 기능을 제공합니다.
2, excel 읽기
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="java.util.*,java.io.*" %>
<%@ page import="org.apache.poi.poifs.filesystem.*,org.apache.poi.hssf.usermodel.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> Excel </title>
</head>
<body>
<center>
<h2>Jsp excel </h2>
<table border="1" width="100%">
<%
// FileInputStream Excel 
FileInputStream finput=new FileInputStream(application.getRealPath("/")+"book1.xls");
POIFSFileSystem fs=new POIFSFileSystem(finput);
HSSFWorkbook wb=new HSSFWorkbook(fs);
// , sheet
HSSFSheet sheet=wb.getSheetAt(0);
wb.close();
finput.close();
// 
HSSFRow row=null;
// 
HSSFCell cell=null;
short i=0;
short y=0;
// 
for(i=0;i<=sheet.getLastRowNum();i++)
{ 
	out.println("<tr>");
	row=sheet.getRow(i);
	for(y=0;y<row.getLastCellNum();y++)
	{
		cell=row.getCell(y);
		out.println("<td>");
		// 
		switch(cell.getCellType())
		{
		     case HSSFCell.CELL_TYPE_NUMERIC:
		    	 out.println(cell.getNumericCellValue());
		    	 break;
		     case HSSFCell.CELL_TYPE_STRING:
		    	 out.println(cell.getStringCellValue());
		    	 break;
		     case HSSFCell.CELL_TYPE_FORMULA:
		    	 out.println(cell.getNumericCellValue());
		    	 break;
		     default:
		    	 out.println(" ");
		    	 break;
		}
	}
	out.println("</td>");
}
out.println("</tr>");
%>
</table>
</center>
</body>
</html>
3, excel 추가
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="java.util.*,java.io.*" %>
<%@ page import="org.apache.poi.poifs.filesystem.*,org.apache.poi.hssf.usermodel.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> Excel </title>
</head>
<body>
<center>
<h2> Excel </h2>
<%
// FileInputStream Excel 
FileInputStream finput=new FileInputStream("D:\\Jee\\dainchiimage\\book1.xls");
POIFSFileSystem fs=new POIFSFileSystem(finput);
HSSFWorkbook wb=new HSSFWorkbook(fs);
// , sheet
HSSFSheet sheet=wb.getSheetAt(0);
finput.close();
// 
HSSFRow row=null;
// 
HSSFCell cell=null;
short i=4;
// , ( 0 )
row=sheet.createRow(i);
cell=row.createCell((short)0);
cell.setCellValue("UML");
cell=row.createCell((short)1);
cell.setCellValue("40");
cell=row.createCell((short)2);
cell.setCellValue("3");
cell=row.createCell((short)3);
// , 
cell.setCellFormula("B"+(i+1)+"*C"+(i+1));
try{
	FileOutputStream fout=new FileOutputStream(application.getRealPath("/")+"book1.xls");
	wb.write(fout);
	fout.close();
	wb.close();
	out.println(" <a href='book1.xls'>book1.xls</a>");
}catch(IOException e)
{
	out.println(" , :"+e.toString());
}
%>
</center>
</body>
</html>

4. excel 삭제
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="java.util.*,java.io.*" %>
<%@ page import="org.apache.poi.poifs.filesystem.*,org.apache.poi.hssf.usermodel.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> Excel </title>
</head>
<body>
<center>
<h2> Excel </h2>
<%
// FileInputStream Excel 
FileInputStream finput=new FileInputStream(application.getRealPath("/")+"book1.xls");
POIFSFileSystem fs=new POIFSFileSystem(finput);
HSSFWorkbook wb=new HSSFWorkbook(fs);
// , sheet
HSSFSheet sheet=wb.getSheetAt(0);
finput.close();
// 
HSSFRow row=null;
// 
HSSFCell cell=null;
// 3 
row=sheet.getRow((short)2);
if(row!=null)
	sheet.removeRow(row);
try{
	FileOutputStream fout=new FileOutputStream(application.getRealPath("/")+"book1.xls");
	wb.write(fout);
	fout.close();
	out.println(" <a href='book1.xls'>book1.xls</a>");
}catch(IOException e)
{
	out.println(" , :"+e.toString());
}
%>
</center>
</body>
</html>
4, 포이 패키지와 프로그램 원본 코드는 나의github에 있습니다. 주소:https://github.com/San-Shui/Excel

좋은 웹페이지 즐겨찾기