一)导出
package com.ad.web.servlet.common;
import java.util.List;import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;
import com.ad.web.rmapi.pojo.ExpUser;
/**
* 导出csv处理方法
*
* @author zxkj
*
*/
public class ExpAddrCSV
{
private static final Logger log = Logger.getLogger(ExpAddrCSV.class);
public static String getCVSString(List<ExpUser> lists)
{
StringBuffer sbf = new StringBuffer();
sbf.append("姓名,常用邮件地址,备用邮件地址1,备用邮件地址2,常用手机号码,备用手机号码1,备用手机号码2,固定电话,生日(XXXX-XX-XX),家庭住址,公司名称,备注,所属分组").append("\n");
if(lists!=null)
{
for (int i = 0; i < lists.size(); i++)
{
ExpUser user = lists.get(i);
String name="";
if(user.getFirst_name()!=null && !"".equals(user.getFirst_name()))
{
name=Check.Checkchart(user.getFirst_name());
}
sbf.append(name).append(",");
if( user.getEmail()!=null)
{
int e = 3 - user.getEmail().length;
if (e > 0)
{
for (int m = 0; m < user.getEmail().length; m++)
{
sbf.append(Check.Checkchart(user.getEmail()[m]));
sbf.append(",");
}
for (int n = 0; n < e; n++)
{
sbf.append(" ");
sbf.append(",");
}
}
else
{
for (int s = 0; s < 3; s++)
{
sbf.append(Check.Checkchart(user.getEmail()[s]));
sbf.append(",");
}
}
}
else
{
for (int y = 0; y < 3; y++)
{
sbf.append("");
sbf.append(",");
}
}
if(user.getMobile_number()!=null)
{
int mo = 3 - user.getMobile_number().size();
if (mo > 0)
{
for (int d = 0; d < user.getMobile_number().size(); d++)
{
sbf.append(Check.Checkchart(user.getMobile_number().get(d)));
sbf.append(",");
}
for (int f = 0; f < mo; f++)
{
sbf.append(" ");
sbf.append(",");
}
}
else
{
for (int g = 0; g < 3; g++)
{
sbf.append(Check.Checkchart(user.getMobile_number().get(g)));
sbf.append(",");
}
}
}
else
{
for (int h = 0; h <3; h++)
{
sbf.append("");
sbf.append(",");
}
}
if(user.getTelephone()!=null && !"".equals(user.getTelephone()))
{
boolean bo=checkTelByZero(user.getTelephone());
if(bo)
{
sbf.append("=\""+Check.Checkchart(user.getTelephone())).append("\",");
}
else
{
sbf.append(Check.Checkchart(user.getTelephone())).append(",");
}
}
else
{
sbf.append("").append(",");
}
if(user.getBirthday()!=null && !"".equals(user.getBirthday()))
{
if("00-00-00".equals(user.getBirthday().trim()))
{
sbf.append("").append(",");
}
else
{
sbf.append(Check.Checkchart(user.getBirthday())).append(",");
}
}
else
{
sbf.append("").append(",");
}
if(user.getHome_address()!=null && !"".equals(user.getHome_address()))
{
sbf.append(Check.Checkchart(user.getHome_address())).append(",");
}
else
{
sbf.append("").append(",");
}
if(user.getCompany_name() != null && !"".equals(user.getCompany_name()))
{
sbf.append(Check.Checkchart(user.getCompany_name())).append(",");
}
else
{
sbf.append("").append(",");
}
if(user.getMemo() != null && !"".equals(user.getMemo()))
{
sbf.append(Check.Checkchart(user.getMemo())).append(",");
}
else
{
sbf.append("").append(",");
}
if(user.getGroupName() != null && !"".equals(user.getGroupName()))
{
sbf.append(Check.Checkchart(user.getGroupName())).append(",");
}
else
{
sbf.append("").append(",");
}
sbf.append("\n");
}
}
return sbf.toString();
}
public static boolean checkTelByZero(String telephone)
{
boolean flag = false;
if ("".equals(telephone) || telephone == null)
{
telephone = "";
}
try
{
Pattern p = Pattern.compile("^0\\d*$");
Matcher m = p.matcher(telephone);
flag = m.matches();
}
catch (Exception e)
{
log.error("retur=failure | telephone="+telephone, e);
}
return flag;
}
}
二)导入
package com.ad.web.servlet.common;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import com.ad.web.rmapi.request.AddUerReq;
import com.csvreader.CsvReader;
/**
* 读取导入文件的人员信息,保存在内存当中
* @author zxkj
*
*/
public class ImpAddrCSV
{
private static final Logger log = Logger.getLogger(ImpAddrCSV.class);
public static List<AddUerReq> ProcessCvsString(String path)
throws IOException
{
List<AddUerReq> list = new ArrayList<AddUerReq>();
CsvReader reader = null;
// 初始化CsvReader并指定列分隔符和字符编码
try
{
reader = new CsvReader(path, ',', Charset.forName("GBK"));
// 保存表头信息
List<String> heads = new ArrayList<String>();
if (reader.readRecord())
{
// 读取每行数据以数组形式返回
String[] str = reader.getValues();
if (str != null && str.length > 0)
{
for (int i = 0; i < str.length; i++)
{
heads.add(str[i]);
}
}
}
/**
* 规则说明:现在所循环的每一行的信息和表头对应起来,并将值赋给相关的属性
*/
StringBuffer s_temp = new StringBuffer();
int q=0;
while (reader.readRecord())
{
// 读取每行数据以数组形式返回
String[] str = reader.getValues();
if (str != null && str.length > 0)
{
AddUerReq user = new AddUerReq();
user.setMarkid(q);
q++;
for (int i = 0; i < str.length && i<heads.size(); i++)
{
s_temp.delete(0, s_temp.length());
s_temp.append(heads.get(i).trim());
if ("姓名".equals(s_temp.toString()))
{
user.setName(Check.FileuploadCheckchart(str[i]));
}
else if ("常用邮件地址".equals(s_temp.toString()))
{
user.setEmail(Check.FileuploadCheckchart(str[i]));
}
else if ("备用邮件地址1".equals(s_temp.toString()))
{
user.setEmail1(Check.FileuploadCheckchart(str[i]));
}
else if ("备用邮件地址2".equals(s_temp.toString()))
{
user.setEmail2(Check.FileuploadCheckchart(str[i]));
}
else if ("常用手机号码".equals(s_temp.toString()))
{
user.setPhone(Check.FileuploadCheckchart(str[i]));
}
else if ("备用手机号码1".equals(s_temp.toString()))
{
user.setPhone1(Check.FileuploadCheckchart(str[i]));
}
else if ("备用手机号码2".equals(s_temp.toString()))
{
user.setPhone2(Check.FileuploadCheckchart(str[i]));
}
else if ("固定电话".equals(s_temp.toString()))
{
user.setTelephone(Check.FileuploadCheckchart(str[i].replace("=", "").replace("\"", "")));
}
else if (s_temp.toString().indexOf("生日")!=-1)
{
user.setBirthday(Check.FileuploadCheckchart(str[i]));
}
else if ("家庭住址".equals(s_temp.toString()))
{
user.setHome_address(Check.FileuploadCheckchart(str[i]));
}
else if ("公司名称".equals(s_temp.toString()))
{
user.setCompany(Check.FileuploadCheckchart(str[i]));
}
else if ("备注".equals(s_temp.toString()))
{
user.setRemark(Check.FileuploadCheckchart(str[i]));
}
else if ("所属分组".equals(s_temp.toString()))
{
user.setGroup(Check.FileuploadCheckchart(str[i]));
}
}
list.add(user);
}
}
}
catch(Exception e)
{
log.error("ProcessCvsString is error",e);
return list;
}
finally
{
if(reader!=null)
{
reader.close();
}
}
return list;
}
/**
* cvs 格式表头信息检验
* 说明:至少有一列为姓名
*/
public static boolean checkHead(String path) throws IOException
{
boolean flag = false;
try
{
CsvReader reader = null;
// 初始化CsvReader并指定列分隔符和字符编码
reader = new CsvReader(path, ',', Charset.forName("GBK"));
// 保存表头信息
List<String> heads = new ArrayList<String>();
if (reader.readRecord())
{
// 读取每行数据以数组形式返回
String[] str = reader.getValues();
if (str != null && str.length > 0)
{
for (int i = 0; i < str.length; i++)
{
heads.add(str[i]);
}
}
}
for (int i = 0; i < heads.size(); i++)
{
if ("姓名".equals(heads.get(i)))
{
flag = true;
return flag;
}
}
}
catch(Exception e)
{
log.error("checkHead is error",e);
return flag;
}
return flag;
}
}