java将数据生成csv文件
Posted brightgreat
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java将数据生成csv文件相关的知识,希望对你有一定的参考价值。
1,httpRequest接口触发进程[或者可以换成其他方式触发]
/**
* 出入库生成CSV文件
* @param req
* @param params
* @return
*/
@RequestMapping(value = "explanCsvFileToOrder")
ResponseMessage explanCsvFileToOrder(HttpServletRequest req, @RequestParam Map<String, String> params) {
TtxSession sess = getSession(req, params.customer)
ResponseMessage rsp
rsp = validate(sess, params)
if (!rsp.hasError()) {
rsp = excfSvc.explanCsvToShipAndRece(sess,params.warehouseCode)
}
return rsp
}
/**
* scheduler_SHIPANDRECE
* @param sess
* @param warehouseCode
* @return
*/
ResponseMessage explanCsvToShipAndRece(TtxSession sess, String warehouseCode) {
ResponseMessage rsp = ResponseMessageFactory.success(sess, null)
try {
//读取全局大文档,找到数据执行的SQL
List invSQLS = template(sess).queryForList("select textValue from large_text where warehouseCode=‘*‘ and type=‘CSV_FILE_SQL‘ and code=‘CSV_SHIPANDRECE‘")
if (!invSQLS) {
return ResponseMessageFactory.error(sess, "未查询到单据条件SQL")
}
//找到存储路径
List filePath = template(sess).queryForList("select value from config_value where warehouseCode=? and identifier=‘CSVFILEPATH‘", warehouseCode)
// String path = "c:/export/"
if (!filePath) {
return ResponseMessageFactory.error(sess, "未查询到存储路径")
}
//定义文件存储路径
String path = filePath[0]["value"].toString()
//定义命名格式
String fileName = "OrderData-" + LocalDateTime.now().format(dtf)
String invSql = invSQLS[0]["textValue"].toString()
//查询出来数据
List locInv = template(sess).queryForList(invSql)
List<Object> exportData = new ArrayList<Object>()
//给csv文件添加标题
exportData.add("DHLWarehouseNo")
exportData.add("StorageNo")
exportData.add("RelatedOrderNo")
exportData.add("ActualWarehouseTime")
exportData.add("SpareColumn1")
exportData.add("SpareColumn2")
exportData.add("LoadNo")
exportData.add("InventoriesAmount")
exportData.add("MaterialExpiryDate")
exportData.add("Department")
exportData.add("MaterialState")
List<List<Object>> datalist = new ArrayList<List<Object>>()
List<Object> data
//循环添加数据主体
locInv?.each {
data = new ArrayList<Object>()
data.add(it.CODE)
data.add(it.WarehouseNo)
data.add(it.deliveryNo)
data.add(it.lastUpdated)
data.add(it.SpareColumn1)
data.add(null)
data.add(it.skuCode)
data.add(it.quantity)
data.add(it.materialExpiryDate)
data.add(it.department)
data.add(it.inventorySts)
datalist.add(data)
}
//创建文件
File file = this.createCSVFile(exportData, datalist, path, fileName)
String fileName2 = file.getName()
System.out.println("文件名称:" + fileName2)
} catch (Throwable t) {
ExceptionManager.logException(sess, t)
return ResponseMessageFactory.error(sess, WmsMessages.MSG_GNRL_0000)
}
return rsp
}
/**
* 生成CSV文件的主体方法
* @param head
* @param dataList
* @param outPutPath
* @param filename
* @return
*/
public static File createCSVFile(List<Object> head, List<List<Object>> dataList, String outPutPath, String filename) {
File csvFile = null
BufferedWriter csvWtriter = null
try {
csvFile = new File(outPutPath + File.separator + filename + ".csv")
File parent = csvFile.getParentFile()
if (parent != null && !parent.exists()) {
parent.mkdirs()
}
csvFile.createNewFile()
// GB2312使正确读取分隔符","
csvWtriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
csvFile), "GB2312"), 1024)
// 写入文件头部
writeRow(head, csvWtriter)
// 写入文件内容
for (List<Object> row : dataList) {
writeRow(row, csvWtriter)
}
csvWtriter.flush()
} catch (Exception e) {
e.printStackTrace()
} finally {
try {
csvWtriter.close()
} catch (IOException e) {
e.printStackTrace()
}
}
return csvFile
}
private static void writeRow(List<Object> row, BufferedWriter csvWriter) throws IOException {
// 写入文件头部
for (Object data : row) {
StringBuffer sb = new StringBuffer()
String rowStr = sb.append(""").append(data).append("",").toString()
csvWriter.write(rowStr)
}
csvWriter.newLine()
}
以上是关于java将数据生成csv文件的主要内容,如果未能解决你的问题,请参考以下文章