jsp页面表格导入Excel中 50
jsp中有一个表格从mssever2000数据库中调用数据现在想有一个按钮点一下自动打开Excel数据自动导入其中页面中的表格加了css代码的页面中显示了颜色边框什么的但...
jsp中有一个表格从ms sever 2000数据库中调用数据
现在想有一个按钮 点一下 自动打开Excel 数据自动导入其中
页面中的表格加了css代码的 页面中显示了颜色 边框什么的
但是导入的只是表格中的纯数据 其它什么都没有
请问怎么实现!!!!!!!
我的意思就是不要导入css啊 展开
现在想有一个按钮 点一下 自动打开Excel 数据自动导入其中
页面中的表格加了css代码的 页面中显示了颜色 边框什么的
但是导入的只是表格中的纯数据 其它什么都没有
请问怎么实现!!!!!!!
我的意思就是不要导入css啊 展开
2个回答
展开全部
使用Apache POI 可以实现,大体框架是这样的,具体可以上csdn搜索相关资料
package skeleton.poi;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
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 skeleton.db.DBFactory;
public class Test {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Test t = new Test();
// t.export();
t.importExcel();
}
/**
* 导出Excel
*
* @throws Exception
*/
public void export() throws Exception {
DBFactory db = new DBFactory();
Connection con = db.connectDB();
Statement stmt = con.createStatement();
String sql = "SELECT t.ID id , t.NAME name , t.TEL tel FROM TEST t ";
ResultSet rs = stmt.executeQuery(sql);
FileOutputStream fos = new FileOutputStream(
"F:\\eclipse-SDK-3.2.1-win32\\eclipse\\workspace\\Skeleton\\attach\\fooExport.xls");
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
// 设置Sheet名
wb.setSheetName(0, "Jie");
List<Model> lst = new ArrayList<Model>();
while (rs.next()) {
Model model = new Model();
model.setId(rs.getInt("id"));
model.setName(rs.getString("name"));
model.setTel(rs.getString("tel"));
lst.add(model);
}
for (short i = 0; i < lst.size(); i++) {
// 创建行
HSSFRow row = s.createRow(i);
for (short j = 0; j < 3; j++) {
// 创建单元格
HSSFCell cell = row.createCell(j);
if (j == 0) {
cell.setCellValue(((Model) lst.get(i)).getId());
} else if (j == 1) {
cell.setCellValue(((Model) lst.get(i)).getName());
} else {
cell.setCellValue(((Model) lst.get(i)).getTel());
}
}
}
wb.write(fos);
fos.close();
}
/**
* 导入Excel
*
* @throws Exception
*/
public void importExcel() throws Exception {
String filename = "F:\\eclipse-SDK-3.2.1-win32\\eclipse\\workspace\\Skeleton\\attach\\fooExport.xls";
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filename));
// 指定Sheet名
HSSFSheet sheet = workbook.getSheet("Jie");
// 行数
int rows = sheet.getPhysicalNumberOfRows();
for (int r = 0; r < rows; r++) {
HSSFRow row = sheet.getRow(r);
if (null != row) {
// 列数
int cells = row.getPhysicalNumberOfCells();
for (short c = 0; c < cells; c++) {
HSSFCell cell = row.getCell(c);
if (null != cell) {
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:
System.out.println(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_STRING:
System.out.println(cell.getStringCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK:
break;
default:
}
}
}
}
}
}
}
package skeleton.poi;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
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 skeleton.db.DBFactory;
public class Test {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Test t = new Test();
// t.export();
t.importExcel();
}
/**
* 导出Excel
*
* @throws Exception
*/
public void export() throws Exception {
DBFactory db = new DBFactory();
Connection con = db.connectDB();
Statement stmt = con.createStatement();
String sql = "SELECT t.ID id , t.NAME name , t.TEL tel FROM TEST t ";
ResultSet rs = stmt.executeQuery(sql);
FileOutputStream fos = new FileOutputStream(
"F:\\eclipse-SDK-3.2.1-win32\\eclipse\\workspace\\Skeleton\\attach\\fooExport.xls");
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
// 设置Sheet名
wb.setSheetName(0, "Jie");
List<Model> lst = new ArrayList<Model>();
while (rs.next()) {
Model model = new Model();
model.setId(rs.getInt("id"));
model.setName(rs.getString("name"));
model.setTel(rs.getString("tel"));
lst.add(model);
}
for (short i = 0; i < lst.size(); i++) {
// 创建行
HSSFRow row = s.createRow(i);
for (short j = 0; j < 3; j++) {
// 创建单元格
HSSFCell cell = row.createCell(j);
if (j == 0) {
cell.setCellValue(((Model) lst.get(i)).getId());
} else if (j == 1) {
cell.setCellValue(((Model) lst.get(i)).getName());
} else {
cell.setCellValue(((Model) lst.get(i)).getTel());
}
}
}
wb.write(fos);
fos.close();
}
/**
* 导入Excel
*
* @throws Exception
*/
public void importExcel() throws Exception {
String filename = "F:\\eclipse-SDK-3.2.1-win32\\eclipse\\workspace\\Skeleton\\attach\\fooExport.xls";
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filename));
// 指定Sheet名
HSSFSheet sheet = workbook.getSheet("Jie");
// 行数
int rows = sheet.getPhysicalNumberOfRows();
for (int r = 0; r < rows; r++) {
HSSFRow row = sheet.getRow(r);
if (null != row) {
// 列数
int cells = row.getPhysicalNumberOfCells();
for (short c = 0; c < cells; c++) {
HSSFCell cell = row.getCell(c);
if (null != cell) {
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:
System.out.println(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_STRING:
System.out.println(cell.getStringCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK:
break;
default:
}
}
}
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询