1
import java.io.File;2
import java.io.FileInputStream;3
import java.io.FileOutputStream;4
import java.io.InputStream;5

6
import jxl.*;7

8
public void mImportList()9


{10
this.importList = new ArrayList();11
try12


{13

14
login me = (login) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("login");15
String userId = String.valueOf(me.getID_());16
String osName = System.getProperty("os.name");17
String storedir = "";18
if(osName == null)19
osName="";20
if(osName.toLowerCase().indexOf("win") != -1)21
storedir = url + userId + "\\";22
else23
storedir = url2 + userId + "/";24
if(main.isDirExists(storedir))25


{26
storedir = storedir + "importExcel.xls";27
File file = new File(storedir);28
if(file.exists())29


{30
InputStream is = new FileInputStream(storedir);31
jxl.Workbook rwb = Workbook.getWorkbook(is);32
Sheet rs = rwb.getSheet(0);33
int rsColumns = rs.getColumns();34
int rsRows = rs.getRows();35
Cell cell;36
String code,name,brand,standard,model,material,color,unit,unit2,price,price2,note;37
boolean isImported;38
for(int i = 1; i39


{40
code=name=brand=standard=model=material=color=unit=unit2=price=price2=note="";41
isImported = false;42

43
cell = rs.getCell(0, i);44
code = cell.getContents();45
cell = rs.getCell(1, i);46
name = cell.getContents();47
cell = rs.getCell(2, i);48
brand = cell.getContents();49
cell = rs.getCell(3, i);50
standard = cell.getContents();51
cell = rs.getCell(4, i);52
model = cell.getContents();53
cell = rs.getCell(5, i);54
material = cell.getContents();55
cell = rs.getCell(6, i);56
color = cell.getContents();57
cell = rs.getCell(7, i);58
unit = cell.getContents();59
cell = rs.getCell(8, i);60
unit2 = cell.getContents();61
cell = rs.getCell(9, i);62
price = cell.getContents();63
cell = rs.getCell(10, i);64
price2 = cell.getContents();65
cell = rs.getCell(11, i);66
note = cell.getContents();67
//68
isImported = mIsRecordExistByCode(code);69
importList.add(70
new product(71
code,72
name,73
brand,74
standard,75
model,76
material,77
color,78
unit,79
unit2, 80
Float.valueOf(price),81
Float.valueOf(price2),82
note,83
isImported84
));85
}86
rwb.close();87
is.close();88
}89
}90
}91

catch(Exception ex)
{ex.printStackTrace();}92
}93

94

95

96
public void mDownload(String path)97


{98
try99


{100
FacesContext ctx = FacesContext.getCurrentInstance();101
ctx.responseComplete();102
//String contentType = "application/octet-stream;charset=utf-8";103
String contentType = "application/x-download";104
HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();105
response.setContentType(contentType);106
StringBuffer contentDisposition = new StringBuffer();107
contentDisposition.append("attachment;");108
contentDisposition.append("filename=\"");109
contentDisposition.append("exportData.xls");110
contentDisposition.append("\"");111
response.setHeader("Content-Disposition", new String(contentDisposition.toString().getBytes(System.getProperty("file.encoding")),"iso8859_1"));112
ServletOutputStream out = response.getOutputStream();113
byte[] bytes = new byte[0xffff];114
InputStream is = new FileInputStream(new File(path));115
int b = 0;116
while ((b = is.read(bytes, 0, 0xffff)) > 0)117


{118
out.write(bytes, 0, b);119
}120
is.close();121
ctx.responseComplete();122

}catch (Exception ex)
{ex.printStackTrace();}123
}