//打印
@RequestMapping("/basicinfo/factory/print.action")
public void print() throws FileNotFoundException, IOException{
/*
* 操作步骤:
* 1、获取数据
* 2、将数据写入到excel文件中
*/
//设置查询条件
Factory factory = new Factory();
factory.setState(1);
List<Factory> dataList = factoryService.find(factory);
String[] title = new String[]{"厂家全称","缩写","联系人","电话","手机","传真","备注"};
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet();
int rowNo = 0; //行号
int colNo = 0; //列号
Row nRow = null;
Cell nCell = null;
sheet.setColumnWidth(0, 30*256); //设置列宽
nRow = sheet.createRow(rowNo);
nRow.setHeightInPoints(40);
sheet.addMergedRegion(new CellRangeAddress(rowNo, rowNo, 0, 6)); //合并单元格,新对象,不会覆盖合并的那些单元格,只是遮住
rowNo++;
nCell = nRow.createCell(0);
nCell.setCellValue("生产厂家通讯录");
nCell.setCellStyle(this.bigTilteStyle(wb));
//写标题
nRow = sheet.createRow(rowNo++);
nRow.setHeightInPoints(28); //设置行高
for(int i=0;i<title.length;i++){
nCell = nRow.createCell(i);
nCell.setCellValue(title[i]);
nCell.setCellStyle(this.titleStyle(wb)); //绑定样式
}
//写数据
for(int j=0;j<dataList.size();j++){
colNo = 0; //初始化
Factory f = dataList.get(j); //获取到每条厂家记录
nRow = sheet.createRow(rowNo++);
nRow.setHeightInPoints(21);
nCell = nRow.createCell(colNo++);
nCell.setCellValue(f.getFullName());
nCell.setCellStyle(this.textStyle(wb));
nCell = nRow.createCell(colNo++);
nCell.setCellValue(f.getFactoryName());
nCell.setCellStyle(this.textStyle(wb));
nCell = nRow.createCell(colNo++);
nCell.setCellValue(f.getContractor());
nCell.setCellStyle(this.textStyle(wb));
nCell = nRow.createCell(colNo++);
nCell.setCellValue(f.getPhone());
nCell.setCellStyle(this.textStyle(wb));
nCell = nRow.createCell(colNo++);
nCell.setCellValue(f.getMobile());
nCell.setCellStyle(this.textStyle(wb));
nCell = nRow.createCell(colNo++);
nCell.setCellValue(f.getFax());
nCell.setCellStyle(this.textStyle(wb));
nCell = nRow.createCell(colNo++);
nCell.setCellValue(f.getCnote());
nCell.setCellStyle(this.textStyle(wb));
}
FileOutputStream os = new FileOutputStream("c:\\factory.xls"); //输出流
wb.write(os); //写入到文件中
os.flush(); //清空缓存
os.close(); //关闭
}
//大标题样式
private CellStyle bigTilteStyle(Workbook wb){
//创建一个单元格样式对象
CellStyle curStyle = wb.createCellStyle();
curStyle.setAlignment(CellStyle.ALIGN_CENTER); //横向居中
curStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); //纵向居中
Font curFont = wb.createFont(); //创建字体对象
curFont.setFontName("华文隶书"); //设置字体
curFont.setFontHeightInPoints((short)30); //设置字体大小
curStyle.setFont(curFont); //将字体对象绑定到样式对象上
return curStyle;
}
//标题样式
private CellStyle titleStyle(Workbook wb){
//创建一个单元格样式对象
CellStyle curStyle = wb.createCellStyle();
curStyle.setAlignment(CellStyle.ALIGN_CENTER); //横向居中
curStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); //纵向居中
Font curFont = wb.createFont(); //创建字体对象
curFont.setFontName("微软雅黑"); //设置字体
curFont.setFontHeightInPoints((short)12); //设置字体大小
curStyle.setFont(curFont); //将字体对象绑定到样式对象上
//画线
curStyle.setBorderTop(CellStyle.BORDER_THIN); //细实线
curStyle.setBorderBottom(CellStyle.BORDER_THIN);
curStyle.setBorderLeft(CellStyle.BORDER_THIN);
curStyle.setBorderRight(CellStyle.BORDER_THIN);
return curStyle;
}
//文本样式
private CellStyle textStyle(Workbook wb){
CellStyle xStyle = wb.createCellStyle();
Font xFont = wb.createFont();
xStyle.setFont(xFont);
xStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); //纵向居中
//画线
xStyle.setBorderTop(CellStyle.BORDER_THIN); //细实线
xStyle.setBorderBottom(CellStyle.BORDER_THIN);
xStyle.setBorderLeft(CellStyle.BORDER_THIN);
xStyle.setBorderRight(CellStyle.BORDER_THIN);
return xStyle;
}
public void print1() throws FileNotFoundException, IOException{
/*
* 操作步骤:
* 1、获取数据
* 2、将数据写入到excel文件中
*/
//创建工作簿
Workbook wb = new HSSFWorkbook();
//创建工作表sheet
Sheet sheet = wb.createSheet();
//创建行对象
Row nRow = sheet.createRow(3); //起始行为0,第四行
//创建单元格对象
Cell nCell = nRow.createCell(1); //第2列
nCell.setCellValue("传智播客!"); //设置单元格内容
FileOutputStream os = new FileOutputStream("c:\\factory.xls"); //输出流
wb.write(os); //写入到文件中
os.flush(); //清空缓存
os.close(); //关闭
}
@RequestMapping("/basicinfo/factory/print.action")
public void print() throws FileNotFoundException, IOException{
/*
* 操作步骤:
* 1、获取数据
* 2、将数据写入到excel文件中
*/
//设置查询条件
Factory factory = new Factory();
factory.setState(1);
List<Factory> dataList = factoryService.find(factory);
String[] title = new String[]{"厂家全称","缩写","联系人","电话","手机","传真","备注"};
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet();
int rowNo = 0; //行号
int colNo = 0; //列号
Row nRow = null;
Cell nCell = null;
sheet.setColumnWidth(0, 30*256); //设置列宽
nRow = sheet.createRow(rowNo);
nRow.setHeightInPoints(40);
sheet.addMergedRegion(new CellRangeAddress(rowNo, rowNo, 0, 6)); //合并单元格,新对象,不会覆盖合并的那些单元格,只是遮住
rowNo++;
nCell = nRow.createCell(0);
nCell.setCellValue("生产厂家通讯录");
nCell.setCellStyle(this.bigTilteStyle(wb));
//写标题
nRow = sheet.createRow(rowNo++);
nRow.setHeightInPoints(28); //设置行高
for(int i=0;i<title.length;i++){
nCell = nRow.createCell(i);
nCell.setCellValue(title[i]);
nCell.setCellStyle(this.titleStyle(wb)); //绑定样式
}
//写数据
for(int j=0;j<dataList.size();j++){
colNo = 0; //初始化
Factory f = dataList.get(j); //获取到每条厂家记录
nRow = sheet.createRow(rowNo++);
nRow.setHeightInPoints(21);
nCell = nRow.createCell(colNo++);
nCell.setCellValue(f.getFullName());
nCell.setCellStyle(this.textStyle(wb));
nCell = nRow.createCell(colNo++);
nCell.setCellValue(f.getFactoryName());
nCell.setCellStyle(this.textStyle(wb));
nCell = nRow.createCell(colNo++);
nCell.setCellValue(f.getContractor());
nCell.setCellStyle(this.textStyle(wb));
nCell = nRow.createCell(colNo++);
nCell.setCellValue(f.getPhone());
nCell.setCellStyle(this.textStyle(wb));
nCell = nRow.createCell(colNo++);
nCell.setCellValue(f.getMobile());
nCell.setCellStyle(this.textStyle(wb));
nCell = nRow.createCell(colNo++);
nCell.setCellValue(f.getFax());
nCell.setCellStyle(this.textStyle(wb));
nCell = nRow.createCell(colNo++);
nCell.setCellValue(f.getCnote());
nCell.setCellStyle(this.textStyle(wb));
}
FileOutputStream os = new FileOutputStream("c:\\factory.xls"); //输出流
wb.write(os); //写入到文件中
os.flush(); //清空缓存
os.close(); //关闭
}
//大标题样式
private CellStyle bigTilteStyle(Workbook wb){
//创建一个单元格样式对象
CellStyle curStyle = wb.createCellStyle();
curStyle.setAlignment(CellStyle.ALIGN_CENTER); //横向居中
curStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); //纵向居中
Font curFont = wb.createFont(); //创建字体对象
curFont.setFontName("华文隶书"); //设置字体
curFont.setFontHeightInPoints((short)30); //设置字体大小
curStyle.setFont(curFont); //将字体对象绑定到样式对象上
return curStyle;
}
//标题样式
private CellStyle titleStyle(Workbook wb){
//创建一个单元格样式对象
CellStyle curStyle = wb.createCellStyle();
curStyle.setAlignment(CellStyle.ALIGN_CENTER); //横向居中
curStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); //纵向居中
Font curFont = wb.createFont(); //创建字体对象
curFont.setFontName("微软雅黑"); //设置字体
curFont.setFontHeightInPoints((short)12); //设置字体大小
curStyle.setFont(curFont); //将字体对象绑定到样式对象上
//画线
curStyle.setBorderTop(CellStyle.BORDER_THIN); //细实线
curStyle.setBorderBottom(CellStyle.BORDER_THIN);
curStyle.setBorderLeft(CellStyle.BORDER_THIN);
curStyle.setBorderRight(CellStyle.BORDER_THIN);
return curStyle;
}
//文本样式
private CellStyle textStyle(Workbook wb){
CellStyle xStyle = wb.createCellStyle();
Font xFont = wb.createFont();
xStyle.setFont(xFont);
xStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); //纵向居中
//画线
xStyle.setBorderTop(CellStyle.BORDER_THIN); //细实线
xStyle.setBorderBottom(CellStyle.BORDER_THIN);
xStyle.setBorderLeft(CellStyle.BORDER_THIN);
xStyle.setBorderRight(CellStyle.BORDER_THIN);
return xStyle;
}
public void print1() throws FileNotFoundException, IOException{
/*
* 操作步骤:
* 1、获取数据
* 2、将数据写入到excel文件中
*/
//创建工作簿
Workbook wb = new HSSFWorkbook();
//创建工作表sheet
Sheet sheet = wb.createSheet();
//创建行对象
Row nRow = sheet.createRow(3); //起始行为0,第四行
//创建单元格对象
Cell nCell = nRow.createCell(1); //第2列
nCell.setCellValue("传智播客!"); //设置单元格内容
FileOutputStream os = new FileOutputStream("c:\\factory.xls"); //输出流
wb.write(os); //写入到文件中
os.flush(); //清空缓存
os.close(); //关闭
}