POIインポートexcel
6433 ワード
1.公式ダウンロード:http://poi.apache.org/download.html またはpomをインポートする
mavenが使用するjarパッケージは、mavenを使用しなければpoi-3.9を使用する.JArとpoi-ooxml-3.9.JAr(これは主にExcel 2007以降のバージョン)学生情報を一括エクスポート私のフロントエンドはeasyuiのdatagrid dgを表のid とします
パッケージUtilクラスpackage com.ysd.util;
import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExcelUtil {
}
mapper
mapper.xml
org.apache.poi
poi
3.6
org.apache.poi
poi-ooxml
3.14
mavenが使用するjarパッケージは、mavenを使用しなければpoi-3.9を使用する.JArとpoi-ooxml-3.9.JAr(これは主にExcel 2007以降のバージョン)学生情報を一括エクスポート私のフロントエンドはeasyuiのdatagrid dgを表のid とします
//
@RequestMapping("/inputStudentExecl")
public void inputStudentExecl(String ids,HttpServletRequest request,HttpServletResponse response) {
List list=null;
if (!ids.equals("")) {
String[] id = ids.split(",");
List list_ids = new ArrayList();
for (int i = 0; i < id.length; i++) {
list_ids.add(Integer.parseInt(id[i]));
}
list = studentService.findStudentByIds(list_ids);
} else {
/*StudentWhere studentWhere2=new StudentWhere();
studentWhere2.setStuName(studentWhere.getStuName());*/
//list=studentService.findAllStdent(new StudentWhere(studentWhere.getStuName(), studentWhere.getPhone(), studentWhere.getQq(), studentWhere.getIsPay(), studentWhere.getIsEffective(), studentWhere.getIsReturn(), studentWhere.getTime(), studentWhere.getStartTime(), studentWhere.getFinishTime()));
list=studentService.findStudent();
}
//
//excel
String[] title = {" "," "," "," "," "," "," "," "," "};
//excel
String fileName = " "+System.currentTimeMillis()+".xls";
//sheet
String sheetName = " ";
//
String[][] content = new String[list.size()][title.length];
for (int i = 0; i < list.size(); i++) {
//
Student obj = list.get(i);
Memberships memberships = list.get(i).getMemberships();
// null
content[i][0] =obj.getCardNo()!=null?obj.getCardNo():null;
content[i][1] = obj.getName()!=null?obj.getName().toString():null;
content[i][2] = obj.getSex()!=null?obj.getSex().toString():null;
content[i][3] = obj.getStatus()!=null?obj.getStatus().toString():null;
content[i][4] = obj.getRemark()!=null?obj.getRemark().toString():null;
content[i][5] = obj.getStuNo()!=null?obj.getStuNo().toString():null;
content[i][6] = memberships.getDepartment()!=null?memberships.getDepartment().toString():null;
content[i][7] = memberships.getSpecialty()!=null?memberships.getSpecialty().toString():null;
content[i][8] = memberships.getDegree()!=null?memberships.getDegree().toString():null;
}
// HSSFWorkbook
HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
//
try {
this.setResponseHeader(response, fileName);//response //fileName
OutputStream os = response.getOutputStream();//FileOutputStream
wb.write(os);//
os.flush();//
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//
public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes(), "ISO8859-1");//
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//
response.setContentType("application/octet-stream;charset=ISO8859-1");
//Content-Type ,
response.setHeader("Content-Disposition", "attachment;filename="
+ fileName);
//
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
パッケージUtilクラスpackage com.ysd.util;
import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExcelUtil {
/**
* Excel
* @param sheetName sheet
* @param title
* @param values
* @param wb HSSFWorkbook
* @return
*/
public static HSSFWorkbook getHSSFWorkbook(String sheetName,String []title,String [][]values, HSSFWorkbook wb){
// , HSSFWorkbook, Excel
if(wb == null){
wb = new HSSFWorkbook();
}
// , workbook sheet, Excel sheet
HSSFSheet sheet = wb.createSheet(sheetName);
// , sheet 0 , poi Excel
HSSFRow row = sheet.createRow(0);
// , ,
HSSFCellStyle style = wb.createCellStyle();
/* style.setAlignment(HSSFCellStyle.ALIGN_CENTER); *///
//
HSSFCell cell = null;
//
for(int i=0;i
}
mapper
/**
* ids
* @param ids id
* @return
*/
List getStudentByIds(List ids);
mapper.xml