jxl实现导出excel代码:
使用jxl导出的xls文件,格式不兼容office 2013,兼容WPS、office 2007
合并单元格使用mergeCells(a,b,c,d)方法:a.单元格的列号,b.单元格的行号,c.从单元格[a,b]起,向下合并的列数,d.从单元格[a,b]起,向下合并的行数
@RequestMapping(value = "/exportPendingManageLibProjs", method = { RequestMethod.GET, RequestMethod.POST })
public void exportPendingManageLibProjs(HttpServletRequest request, HttpServletResponse response) {
PromptMessage promptMessage = getPromptMessage()
response.reset()
// String directory = "e:\\ppp\\test"
String directory = DBConfig.getString("export_doc_directory")
String uuid = UUIDHelper.get32UUID()
String currentDirectory = directory + File.separator + uuid + File.separator
String currentTime = TimestampUtils.formatDate(new Date(), "yyyyMMddHHmmss")
String zipName = "excelfiles_" + currentTime + ".zip"
File dir = new File(currentDirectory)
dir.mkdirs()
response.setContentType("APPLICATION/OCTET-STREAM")
try {
response.setHeader("Content-Disposition", "attachment; filename=" + new String(zipName.getBytes("GBK"), "ISO-8859-1"))
} catch (UnsupportedEncodingException e2) {
e2.printStackTrace()
}
ZipOutputStream outZ = null
try {
outZ = new ZipOutputStream(response.getOutputStream())
} catch (IOException e1) {
e1.printStackTrace()
}
String selectedRows = request.getParameter("selectedRows")
JSONArray jsonArray = JSONArray.fromObject(selectedRows)
WritableWorkbook workbook = null
try {
for (int j = 0
JSONObject jsonObject = jsonArray.getJSONObject(j)
String projRid = jsonObject.getString("PROJ_RID")
String ticketId = jsonObject.getString("TICKETID")
String bpmnType = jsonObject.getString("BPMNTYPE")
String projName = jsonObject.getString("PROJ_NAME")
projName = projName.replace("/", "")
String fileName = "导出待入库_" + projName + ".xls"
// response.setContentType("application/vnd.ms-excel; charset=gb2312")
// response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("GBK"), "ISO-8859-1"))
workbook = Workbook.createWorkbook(new File(currentDirectory + File.separator + fileName))
Map param = new HashMap()
param.put("projRid", projRid)
List
ZipUtils类代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
/**
* 压缩文件工具类
*/
public class ZipUtils {
public static void doCompress(String srcFile, String zipFile) throws Exception {
doCompress(new File(srcFile), new File(zipFile));
}
/**
* 文件压缩
* @param srcFile 目录或者单个文件
* @param destFile 压缩后的ZIP文件
*/
public static void doCompress(File srcFile, File destFile) throws Exception {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(destFile));
if(srcFile.isDirectory()){
File[] files = srcFile.listFiles();
for(File file : files){
doCompress(file, out);
}
}else {
doCompress(srcFile, out);
}
}
public static void doCompress(String pathname, ZipOutputStream out) throws IOException{
doCompress(new File(pathname), out);
}
public static void doCompress(File file, ZipOutputStream out) throws IOException{
if( file.exists() ){
byte[] buffer = new byte[1024];
FileInputStream fis = new FileInputStream(file);
out.putNextEntry(new ZipEntry(file.getName()));
int len = 0 ;
while ((len = fis.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.setEncoding("gbk");
out.flush();
out.closeEntry();
fis.close();
}
}
}
前台使用Form提交方式:
exportPendingManageLibProjs : function() {
var selectedRows = jkt.table.getSelectedRows();
if (selectedRows.length == 0) {
jkt.dialogError("请选择要导出的项目");
return;
}
var url = "/ppp/projectAudit/exportPendingManageLibProjs.do?TT_TOKEN=" + jkt.getCookie("TT_TOKEN");
$("#selectedRows").val(JSON.stringify(selectedRows));
$("#myExportManageLibForm").attr("action", jkt.realPath(url));
$("#myExportManageLibForm").submit();
}