import java.io.File
import java.io.FileOutputStream
import java.sql.Connection
import java.sql.DriverManager
import java.sql.ResultSet
import java.sql.Statement
import java.util.ArrayList
import java.util.HashMap
import java.util.List
import java.util.Map
import org.apache.poi.hssf.usermodel.HSSFCell
import org.apache.poi.hssf.usermodel.HSSFCellStyle
import org.apache.poi.hssf.usermodel.HSSFFont
import org.apache.poi.hssf.usermodel.HSSFRow
import org.apache.poi.hssf.usermodel.HSSFSheet
import org.apache.poi.hssf.usermodel.HSSFWorkbook
import org.apache.poi.hssf.util.HSSFColor
import com.ewin.common.DBException
import com.ewin.util.ExcelUtil
import com.ewin.util.StringUtil
public class TestExl {
@SuppressWarnings("unchecked")
public static void main(String[] args)throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver")
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.90:1521:a1", "bill", "bill")
Statement state = conn.createStatement()
try {
// 导入时模型羡迹板头部信息
String[] headNames = new String[] { "运单号", "运单ID"}
// 头部列对应的字段名
String[] fieldNames = new String[] { "BILL_NO", "BILL_ID"}
Map<String, String>卜并 columnMap = new HashMap<String, String>派念()
columnMap.put("BILL_NO", "运单号")
List<Object>returnList = ExcelUtil.getExcelToList(columnMap,""
+ "D:\\Documents\\Desktop\\exs.xls", 0)
List<Object>list = new ArrayList<Object>()
for (int i = 0i <returnList.size()i++) {
Map<Object,Object>map = (Map<Object,Object>) returnList.get(i)
// 如果必填项为空,则提示信息,不能导入
ResultSet result = state.executeQuery("select ser_bill_no bill_id from op_bill where bill_no = '"+map.get("BILL_NO").toString()+"'")
Map map1 = new HashMap()
map1.put("BILL_NO",map.get("BILL_NO"))
String bill_id = ""
while(result.next()){
bill_id = result.getString(1)
}
map1.put("BILL_ID", bill_id)
list.add(map1)
}
Map m = new HashMap()
m.put("LIST", list)
m.put("HEAD", headNames)
m.put("FIELD", fieldNames)
createExcel(m)
} catch (DBException e) {
throw e
}
state.close()
conn.close()
System.out.println("生成完毕")
}
@SuppressWarnings("unchecked")
public static void createExcel(Map map){
try{
// 获取要导出的数据集
List list = (List) map.get("LIST")
// excel头部
String[] headNames = (String[]) map.get("HEAD")
// excel头部对应的表字段
String[] fieldNames = (String[]) map.get("FIELD")
// 创建Excel的工作书册 Workbook,对应到一个excel文档
HSSFWorkbook wb = new HSSFWorkbook()
// 页数
int pages = 1
// 获取总页数
if (list.size() >50000) {
if (list.size() % 50000 == 0)
pages = list.size() / 50000
else
pages = list.size() / 50000 + 1
}
// 数据下标
int b = 0
// 循环页数
for (int k = 1k <= pagesk++) {
/***************** 创建页 *****************/
// 创建Excel的工作sheet,对应到一个excel文档的tab
HSSFSheet sheet = wb.createSheet("第" + k + "页")
// 设置excel每列宽度
sheet.setColumnWidth(0, 4000)
sheet.setColumnWidth(1, 3500)
// 创建字体样式
HSSFFont font = wb.createFont()
font.setFontName("Verdana")
font.setBoldweight((short) 100)
font.setFontHeight((short) 300)
font.setColor(HSSFColor.BLUE.index)
// 创建单元格样式
HSSFCellStyle style = wb.createCellStyle()
style.setAlignment(HSSFCellStyle.ALIGN_CENTER)
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER)
// 设置边框
style.setBorderBottom(HSSFCellStyle.BORDER_THIN)
style.setBorderLeft(HSSFCellStyle.BORDER_THIN)
style.setBorderRight(HSSFCellStyle.BORDER_THIN)
style.setBorderTop(HSSFCellStyle.BORDER_THIN)
/*********** 创建页 ****************/
/***************** 创建标题和表头 *****************/
// 创建行
HSSFRow row = sheet.createRow(0)
row.setHeight((short) 500)// 设定行的高度
// 循环创建标题列
for (int i = 0i <headNames.lengthi++) {
// 创建一个Excel的单元格
HSSFCell cell = row.createCell(i)
cell.setCellStyle(style)
cell.setCellValue(headNames[i])
}
/*********** 创建标题和表头 ****************/
/*********** 添加内容 ****************/
// 行
int i = 0
for (b <list.size()b++) {
// 起始为第2行
i++
// 创建行
row = sheet.createRow((i))
row.setHeight((short) 500)// 设定行的高度
// 获取该行应输出的对象map
Map tempMap = (Map) list.get(b)
for (int j = 0j <fieldNames.lengthj++) {
// 创建一个Excel的单元格
HSSFCell cell = row.createCell(j)
cell.setCellStyle(style)
// 取该列对应的值
Object temp = tempMap.get(fieldNames[j])
if (StringUtil.isNull(temp))
cell.setCellValue("")
else {
if (fieldNames[j].equals("BILL_NO")) {
cell.setCellValue(temp.toString())
} else {
try {
cell.setCellValue(Double.parseDouble(temp
.toString()))
} catch (Exception e) {
cell.setCellValue(temp.toString())
}
}
}
}
// 一页写完
if (b % 50000 == 0 &&b != 0) {
b++
break
}
}
/*********** 添加内容 ****************/
}
wb.write(new FileOutputStream(new File("D:\\Documents\\Desktop\\exscop.xls")))
}catch(Exception e){
e.printStackTrace()
}
}
}
我这个案例你可以看下、
你直接new一拦弯个相同文件就会将旧文件清空。州衡逗比如:册卖OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(outputPath), "utf-8")
Java使用FileWriter实现文件的写入,用法为:FileWriter(file,true)其中第二个参数设置成false就是覆盖写入,true就是增量存储。举棚型例代码:
import java.io.Fileimport java.io.FileWriter
import java.io.IOException
public class File01Demo {
public static void main(String[] args) throws IOException {
核搭 File file = new File("D:\\a.txt")
FileWriter fw = new FileWriter(file,true) //设置成改和拿true就是追加
fw.write("asd")
fw.write("\r\n")
fw.write("ffd")
fw.close()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)