ajax传递excel后台接收,前端上传EXCEL文件,后台servlet怎么获取EXCEL中的数据
//excel文件
private File xlsFile;
//excel列名
private static final String[] SALARYIMPORTVALUE_COLUMN_NAMES = { “機(jī)組名稱”,“顯示順序”, “備注”};
/**
* 從excel文件導(dǎo)入操作任務(wù) add by ytrao 20111202
* @throws IOException
*/
public void importWoTickTask() throws IOException{
String parentBlockId = request.getParameter("parentBlockId");
String code = remote.findMaxCode(parentBlockId);
InputStream is=null;
Workbook workbook=null;
RunCWorkticketBlock opTask = null;
String msg = "";
List opTaskList = new ArrayList();
try {
is = new FileInputStream(xlsFile);
workbook = Workbook.getWorkbook(is);// 得到工作薄
Sheet sheet = workbook.getSheet(0);// 得到工作薄中的第一個(gè)工作表
int rows = sheet.getRows();// 得到excel的總行數(shù)
if (rows == 0) {
msg = "{success:true,msg:'無(wú)數(shù)據(jù)進(jìn)行導(dǎo)入!'}";
write(msg);
} else if (rows == 1) {
msg = "{success:true,msg:'文件除一列頭行外,至少還需一行數(shù)據(jù)!'}";
write(msg);
} else {
int[] column_indexs = new int[sheet.getRow(0).length];
String columnError = null;
columnError = addColumnIndex(column_indexs, sheet.getRow(0));
// 列名是否存在問(wèn)題
if (columnError != null){
write(columnError);
return;
}
outer:
for (int i = 1; i < rows; i++) {
opTask = new RunCWorkticketBlock();
Cell[] cells = sheet.getRow(i);// i行的所有單元格
if(cells.length > column_indexs.length){
i++;
msg = "第" + (i+1)+ "行的數(shù)據(jù)超過(guò)列頭行!
";
break;
}
for (int j = 0; j < column_indexs.length; j++) {
column_indexs[j] = j;
//0 : 序號(hào)
// 1:機(jī)組名稱
if (column_indexs[j] == 0) {
if (cells.length > j
&& !"".equals(cells[j].getContents())) {
for (RunCWorkticketBlock entity : opTaskList) {
if (cells[j].getContents().equals(entity.getBlockName())) {
msg+= "Excel中第" +(i+1) + "行機(jī)組名稱重復(fù)!
";
continue outer;
}
}
String list = remote.findByPId(parentBlockId);
if(list !=null&& list !=""){
//System.out.println(list);
String arry[] = list.split(",");
//System.out.println(“555555” +arry[0]);
for(int a=arry.length-1;a>=0;a–){
if (cells[j].getContents().equals(arry[a])) {
msg+= "Excel中第" +(i+1) + "行機(jī)組名稱已經(jīng)存在!
";
continue outer;
}
}
}
opTask.setBlockName(cells[j].getContents());
}else{
continue outer;
}
}
// 2:顯示序號(hào)
else if (column_indexs[j] == 1) {
if (cells.length > j&& !"".equals(cells[j].getContents())) {
opTask.setDisplayNo(Long.parseLong(cells[j].getContents()));
}
}
// 3:備注
else if (column_indexs[j] == 2) {
if (cells.length > j
&& !"".equals(cells[j].getContents())) {
opTask.setMemo(cells[j].getContents());
}
}
}
opTask.setParentBlockId(Long.parseLong(parentBlockId));
opTask.setLastModifyBy(employee.getWorkerCode());
opTask.setLastModifyDate(new Date());
opTask.setIsUse("Y");
opTaskList.add(opTask);
}
if(msg.equals(""))
{
for(RunCWorkticketBlock entity : opTaskList){
remote.save(entity);
}
write("{success:true,msg:'導(dǎo)入成功!'}");
}
else
{
write("{success:true,msg:'數(shù)據(jù)填寫存在問(wèn)題或機(jī)組名稱重復(fù),請(qǐng)確認(rèn)后在導(dǎo)入,
"+msg+"'}");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
workbook.close();
is.close();
}
}
/**
* 檢測(cè)導(dǎo)入的Excel文件列名是否正確
* @param column_indexs
* @param cells
* @return String
*/
private String addColumnIndex(int[] column_indexs, Cell[] cells) {
for (int i = 0; i < cells.length; i++) {
boolean isError = true;
for (int j = 0; j < SALARYIMPORTVALUE_COLUMN_NAMES.length; j++) {
if (SALARYIMPORTVALUE_COLUMN_NAMES[j].equals(cells[i].getContents())) {
column_indexs[i] = j;
isError = false;
}
}
if (isError) {
return "{success:true,msg:'" + cells[i].getContents()
+ "列不是要導(dǎo)入的具體列!'}";
}
}
return null;
}
public File getXlsFile() {
return xlsFile;
}
public void setXlsFile(File xlsFile) {
this.xlsFile = xlsFile;
}
以前做傳統(tǒng)行業(yè)開發(fā),用到的導(dǎo)入模型
總結(jié)
以上是生活随笔為你收集整理的ajax传递excel后台接收,前端上传EXCEL文件,后台servlet怎么获取EXCEL中的数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: jquery ajax node,jqu
- 下一篇: ajax连接云数据库密码,ajax和数据