在Java语言中如何实现导入和导出Excel表,详细说下。
解决方案 »
- 什么地方做java开发待遇好
- 求Struts和Hibernate结合增删改查功能源码
- java有什么好的方法计算word页数 poi好像不准 急!!!
- 有关Junit
- rmi 调试出错 java.rmi.UnmarshalException: error unmarshalling arguments
- 2道面试题
- ******紧急求助******JDBC在获取数据库字段类型后怎么区分整型与浮点类型!
- 问题:struts标签的使用 服务器报错 javax.servlet.ServletException: Cannot find bean under name org.apache.struts.taglib.html.BEAN
- 關於EJB的問題,各位高手請指教!先謝了
- jackson 使用writeValueAsString() 怎么保留date类型的时间
- java实现CRC-CCITT(Kermit)校验码的功能
- easyValidata拖动时的bug
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
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;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;//Source file: D:\\Excell.java
public class Excell implements IExcell
{
/**
* @roseuid 45B58EFC03A9
*/
public Excell()
{
}
/**
* @param sheetName
* @param path
* @param ges
* @return String
* @roseuid 45B58EFC03D8
*/
public String xie(String sheetName, String path, String[][] ges)
{
// 新建文件
HSSFWorkbook wb = new HSSFWorkbook();
// 新建工作表
HSSFSheet sheet = wb.createSheet(sheetName);
for (int hang = 0; hang <ges.length; hang++)
{
// 创建行
HSSFRow row = sheet.createRow((short)hang);
for (int lie = 0; lie <ges[hang].length; lie++)
{
HSSFCell cell = row.createCell((short) lie);// 创建格 createCell((short) lie)
cell.setCellValue(ges[hang][lie]);
}
}
try
{
FileOutputStream fileout = new FileOutputStream(path);
wb.write(fileout);
fileout.close();
return null;
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return path;
}
public String[][] du(String path, String sheetName)
{
try
{ POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream( path));
// 文件
HSSFWorkbook wb = new HSSFWorkbook(fs);
//工作表
HSSFSheet sheet = wb.getSheetAt(0); // 行数
int hb = sheet.getPhysicalNumberOfRows();
// 列数
int lb = sheet.getRow(0).getPhysicalNumberOfCells();
String[][] result=new String[hb][lb];
for (int h = 0; h<hb ; h++)
{
// 获得工作表的行
HSSFRow row = sheet.getRow(h); for (int l= 0;l<lb ; l++)
{
// 获得工作表的列
HSSFCell cell = row.getCell((short)l);
result[h][l]=cell.toString() ;
}
System.out.println();
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
}
{
/**
* @param sheetName
* @param path
* @param ges
* @return String
* @roseuid 45B58D010138
*/
public String xie(String sheetName, String path, String[][] ges);
public String[][] du(String path, String sheetName);
}
import junit.framework.TestCase;public class ExcellTest extends TestCase
{ public void testXie()
{
String[][] ges=
{
{"aa","vv"},
{"cc","dd"}
} ;
IExcell k=new Excell();
k.xie("s1", "c:/tt.xls", ges);
} public void testDu()
{
IExcell a=new Excell();
a.du("c:/tt.xls", "s1");
}
记着下载,jxl.jar包。package student;import java.io.IOException;
import java.io.OutputStream;
import java.sql.ResultSet;
import java.sql.SQLException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;import database.DBConn;/**
* 学生成绩管理中,打印功能
* @author ss
*
*/
public class ConvertToExcel2 extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
exportExcel(response,request);
} catch (SQLException e) {
e.printStackTrace();
}
} public void exportExcel(HttpServletResponse response,HttpServletRequest request) throws IOException,
SQLException {
try{
OutputStream os = response.getOutputStream();// 取得输出流
response.reset();// 清空输出流
response.setCharacterEncoding("utf8");
response.setHeader("Content-disposition", "attachment; filename="
+ "chengji" + ".xls");// 设定输出文件头
response.setContentType("application/msexcel");// 定义输出类型 WritableWorkbook wbook = Workbook.createWorkbook(os); // 建立excel文件
WritableSheet wsheet = wbook.createSheet("sheet1", 0); // sheet名称 // 设置excel标题
WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE);
WritableCellFormat wcfFC = new WritableCellFormat(wfont);
wcfFC.setAlignment(Alignment.CENTRE); // 开始生成主体内容
wsheet.addCell(new Label(0, 0, "姓名"));
wsheet.addCell(new Label(1, 0, "学期"));
wsheet.addCell(new Label(2, 0, "年级"));
wsheet.addCell(new Label(3, 0, "班级科目"));
wsheet.addCell(new Label(4, 0, "考试次数"));
wsheet.addCell(new Label(5, 0, "成绩"));
int i = 1;
wsheet.addCell(new Label(0, i, "测试"));
wsheet.addCell(new Label(1, i, "测试"));
wsheet.addCell(new Label(2, i, "测试"));
wsheet.addCell(new Label(3, i, "测试"));
wsheet.addCell(new Label(4, i, "测试"));
wsheet.addCell(new Label(5, i, "测试"));
i++;
rs1.close();
db.close(); // 主体内容生成结束
wbook.write(); // 写入文件
wbook.close();
os.flush();
os.close(); // 关闭流
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
不过 要注意03版本 和07版本 用的类不一样