一:给一个字符串中的各个词加上单引号
Java基础学习,更多资源 http://www.itkc8.com
public static String strDYH(String strpara){
String[] a = strpara.split(",");//把字符串变成数组
StringBuilder sb = new StringBuilder();
for(int i=0;i<a.length;i++){
sb.append("\'"+a[i]+"\',");
}
String c = sb.toString().trim();
String tp = c.substring(c.length()-1,c.length());
if(",".equals(tp)){
return c.substring(0,c.length()-1).toString();
}else{
return c.toString();
}
}
二:字符串转成数组
public List changList(String str) {
String[] arrayStr = new String[] {};
List<String> list = new ArrayList<String>();
arrayStr = str.split(",");
return java.util.Arrays.asList(arrayStr);
三:缓存
//list放入session缓存起来导出之用
HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST);
request.setCharacterEncoding("UTF-8");
// String name = request.getAttribute("name");
HttpSession session=request.getSession();
session.setAttribute("listCompanyInfo", listAll);
四:list2给另一个list1
List list1 = new ArrayList<CompanyAndComposition>(list2);
五:json进行前后台交互的封装类
/**
* @author HUXU
*
*/
public class BaseAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
public String getJsonString(Object o){
ObjectMapper om=new ObjectMapper();
StringWriter sw = new StringWriter();
try {
JsonGenerator jg = new JsonFactory().createJsonGenerator(sw);
om.writeValue(jg, o);
} catch (JsonGenerationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sw.toString();
}
public void writeJson(Object o){
String json = getJsonString(o);
try {
ServletActionContext.getResponse().getWriter().write(json);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
*/
public BaseAction() {
// TODO Auto-generated constructor stub
}
}
//调用对象变成json类
writeJson(listAll);
六:中文过滤器
public class Zh_Filter implements Filter{
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//将所有request内的对象设置字符集为gb2312
request.setCharacterEncoding("UTF-8");
//将所有response内的对象设置字符集为gb2312
response.setCharacterEncoding("UTF-8");
//用chain的doFilter处理过滤
chain.doFilter(request, response);
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
七:导出数据表格的
package com.newsky.action;
import com.newsky.domain.CompanyAndComposition;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
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 org.apache.struts2.StrutsStatics;
public class ExportExcel extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
@SuppressWarnings("unchecked")
public InputStream getInputStream() {
Map<String, Object> session = ActionContext.getContext().getSession();
System.out.println(session);
List<CompanyAndComposition> list = (List<CompanyAndComposition>) session
.get("listCompanyInfo");
System.out.println("缓存中的" + list);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("sheet1");
// String sheet = new String(sheet.getBytes("ISO-8859-1"), "UTF-8");
//
int rowNum = 0; // 标题开始行
int colNum = 0; // 标题开始列
// HSSFRow excell的头
HSSFRow row = sheet.createRow(rowNum++);
// HSSFCell excell的格子单元
HSSFCell cell = row.createCell((short) colNum++);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell中文编码;
cell.setCellValue("时间");
cell = row.createCell((short) colNum++);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell中文编码;
cell.setCellValue("公司");
cell = row.createCell((short) colNum++);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell中文编码;
cell.setCellValue("代号");
if (null == list || list.size() == 0) {
// HttpServletRequest request = (HttpServletRequest)
// ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST);
// try {
// // request.setCharacterEncoding("UTF-8");
// // request.setAttribute("datanull", -1);//页面回传值
System.out.println("list是空的");
// } catch (UnsupportedEncodingException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
} else {
for (CompanyAndComposition user : list) {
row = sheet.createRow(rowNum++);
colNum = 0;
cell = row.createCell((short) colNum++);
cell.setCellValue(user.getOrder_time());
cell = row.createCell((short) colNum++);
cell.setCellValue(user.getSortno());
cell = row.createCell((short) colNum++);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell中文编码;
cell.setCellValue(user.getDm_company());
}
}
File file = new File("abc.xls");
// 创建一个File 拿来当缓存用.也就是先将内存中的excel写入File中.然后再将File转换成输出流
try {
OutputStream out = new FileOutputStream(file);
wb.write(out);// 写入File
out.close();
} catch (Exception e) {
e.printStackTrace();
}
InputStream in = null;
try {
in = new FileInputStream(file);// 将file转换成输入流
// in.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return in;
}
public String execute() throws Exception {
try {
getInputStream();
return SUCCESS;
} catch (Exception e1) {
e1.printStackTrace();
return ERROR;
}
}
}
在struts.xml中的配置
<action name="excel" class="com.newsky.action.ExportExcel">
<result name="success" type="stream">
<param name="contentType">application/vnd.ms-excel;charset=UTF-8</param>
<param name="contentDisposition">attachment;filename="DmReport.xls"</param>
<param name="inputName">inputStream</param>
</result>
</action>
Java基础学习,更多资源 http://www.itkc8.com