【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
**开源地址:https://docs.qq.com/doc/DSmxTbFJ1cmN1R2dB **
style_date.setDataFormat(df.getFormat(“yyyy-MM-dd HH:mm:ss”));
// 设置标题的样式
HSSFCellStyle style_title = wb.createCellStyle();
style_title.setAlignment(HorizontalAlignment.CENTER);
style_title.setVerticalAlignment(VerticalAlignment.CENTER);
HSSFFont style_font = wb.createFont();
style_font.setFontName(“黑体”);
style_font.setFontHeightInPoints((short) 18);
// 加粗
style_font.setBold(true);
style_title.setFont(style_font);
// 创建内容样式的字体
HSSFFont font_content = wb.createFont();
// 设置字体名称,相当于选中了那种字符
font_content.setFontName(“宋体”);
// 设置字体的大小
font_content.setFontHeightInPoints((short) 11);
style_content.setFont(font_content);
// 合并单元格
// 合并:标题
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3));
// 合并第二行
sheet.addMergedRegion(new CellRangeAddress(2, 2, 1, 3));
// 合并第7行
sheet.addMergedRegion(new CellRangeAddress(7, 7, 0, 3));
// 创建11行,4列
// 创建矩阵11行,4列
int rowCount = detaillist.size() + 9;// 创建单元格的数量是数据的长度+9
for (int i = 2; i <= rowCount; i++) {
row = sheet.createRow(i);
for (int j = 0; j < 4; j++) {
// 给单元格设置样式
row.createCell(j).setCellStyle(style_content);
}
}
// 必须先有创建的行和单元格,才可以使用
// 创建标题单元格
HSSFCell titleCell = sheet.createRow(0).createCell(0);
titleCell.setCellValue(sheetName);
// 设置标题样式
titleCell.setCellStyle(style_title);
sheet.getRow(2).getCell(0).setCellValue(“供应商”);
sheet.getRow(3).getCell(0).setCellValue(“下单日期”);
sheet.getRow(4).getCell(0).setCellValue(“审核日期”);
sheet.getRow(5).getCell(0).setCellValue(“采购日期”);
sheet.getRow(6).getCell(0).setCellValue("入库日期 ");
sheet.getRow(3).getCell(2).setCellValue(“经办人”);
sheet.getRow(4).getCell(2).setCellValue(“经办人”);
sheet.getRow(5).getCell(2).setCellValue(“经办人”);
sheet.getRow(6).getCell(2).setCellValue(“经办人”);
sheet.getRow(7).getCell(0).setCellValue(“订单明细”);
sheet.getRow(8).getCell(0).setCellValue(“商品名称”);
sheet.getRow(8).getCell(1).setCellValue(“商品数量”);
sheet.getRow(8).getCell(2).setCellValue(“商品价格”);
sheet.getRow(8).getCell(3).setCellValue(“金额”);
// 设置行高于列宽
// 标题行高
sheet.getRow(0).setHeight((short) 1000);
// 内容体的行高
for (int i = 2; i <= rowCount; i++) {
sheet.getRow(i).setHeight((short) 500);
}
// 设置列宽
for (int i = 0; i < 4; i++) {
sheet.setColumnWidth(i, (short) 5000);
}
// 订单详情,设置日期与经办人
// 设置单元格样式
sheet.getRow(3).getCell(1).setCellStyle(style_date);
sheet.getRow(4).getCell(1).setCellStyle(style_date);
sheet.getRow(5).getCell(1).setCellStyle(style_date);
sheet.getRow(6).getCell(1).setCellStyle(style_date);
if(null != orders.getCreatetime()){
sheet.getRow(3).getCell(1).setCellValue(orders.getCreatetime());
}
if(null != orders.getChecktime()){
sheet.getRow(4).getCell(1).setCellValue(orders.getChecktime());
}
if(null != orders.getStarttime()){
sheet.getRow(5).getCell(1).setCellValue(orders.getStarttime());
}
if(null != orders.getEndtime()){
sheet.getRow(6).getCell(1).setCellValue(orders.getEndtime());
}
// 缓存员工编号的名称,key=员工编号,value=员工名称
Map<Long, String> empNameMap = new HashMap<Long, String>();
// 设置经办人
sheet.getRow(3).getCell(3).setCellValue(getEmpName(orders.getCreater(), empNameMap, empDao));
sheet.getRow(4).getCell(3).setCellValue(getEmpName(orders.getStarter(), empNameMap, empDao));
sheet.getRow(5).getCell(3).setCellValue(getEmpName(orders.getStarter(), empNameMap, empDao));
sheet.getRow(6).getCell(3).setCellValue(getEmpName(orders.getEnder(), empNameMap, empDao));
// 缓存供应商编号与员工的名称,key=供应商的编号,value=供应商的名称
Map<Long, String> supplierNameMap = new HashMap<Long, String>();
// 设置供应商
sheet.getRow(2).getCell(1).setCellValue(getSupplierName(orders.getSupplieruuid(), supplierNameMap));
// 将数据库当中的数据填充(设置明细内容)
int index = 0;
Orderdetail od = null;
for (int i = 9; i < rowCount; i++) {
od = detaillist.get(index);
row = sheet.getRow(i);
row.getCell(0).setCellValue(od.getGoodsname());
row.getCell(1).setCellValue(od.getNum());
row.getCell(2).setCellValue(od.getPrice());
row.getCell(3).setCellValue(od.getMoney());
index++;
}
sheet.getRow(rowCount).getCell(0).setCellValue(“合计”);
// 设置合计
sheet.getRow(rowCount).getCell(3).setCellValue(“” + orders.getTotalmoney());
// 写到输出流当中
try {
wb.write(os);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
wb.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2、导出订单后端OrdersAction当中
3、导出订单前端实现
(1)在orders.html当中引入download.js
(2)修改orders.js
//添加审核按钮
var toolbar = new Array();
toolbar.push({
text:‘导出’,
iconCls:‘icon-excel’,
handler:doExport
})
if(Request[‘oper’] == ‘doCheck’ ){
toolbar.push({
toolbar:[{
text:‘审核’,
iconCls:‘icon-search’,
handler:doCheck
}]
});
}
//添加确认按钮
if(Request[‘oper’] == ‘doStart’ ){
toolbar.push({
toolbar:[{
text:‘确认’,
iconCls:‘icon-search’,
handler:doStart
}]
});
}
$(‘#ordersDlg’).dialog({
toolbar:toolbar
});
function doExport(){
KaTeX parse error: Expected '}', got 'EOF' at end of input: …_export",{"id":(‘#uuid’).html()});
}
我们需要将批量的供应商(客户)信息导入到系统里,因此我们需要实现导入功能
如下图:点击导入按钮
弹出导入数据对话框:
选择excel文件后点击“导入”按钮,把数据导入到系统中,成功后刷新表格并关闭“导入数据”窗口
1、修改SupplierDao
2、后端代码实现,在erp_biz下的ISupplier和Supplier
(1)ISupplier
(2)SupplierBiz当中
@Override
public void doImport(InputStream is) throws IOException {
HSSFWorkbook wb = null;
try {
wb = new HSSFWorkbook(is);
HSSFSheet sheet = wb.getSheetAt(0);
String type = “”;
if(“供应商”.equals(sheet.getSheetName())) {
type = Supplier.TYPE_SUPPLIER;
}else if(“客户”.equals(sheet.getSheetName())) {
type = Supplier.TYPE_CUSTOMER;
}else {
throw new ErpException(“工作表名称不正确”);
}
//读取数据
int lastRow = sheet.getLastRowNum();//获取最后一行的行号
Supplier supplier = null;
for (int i = 1; i <= lastRow; i++) {
supplier = new Supplier();
supplier.setName(sheet.getRow(i).getCell(0).getStringCellValue());//设置供应商名称
//判断是否已经存在,通过名称来判断
List list = supplierDao.getList(null, supplier, null);
if(list.size() > 0) {
supplier = list.get(0);
}
supplier.setAddress(sheet.getRow(i).getCell(1).getStringCellValue());//设置地址
supplier.setContact(sheet.getRow(i).getCell(2).getStringCellValue());//联系人
supplier.setTele(sheet.getRow(i).getCell(3).getStringCellValue());//联系电话
supplier.setEmail(sheet.getRow(i).getCell(4).getStringCellValue());//邮箱
if(list.size()==0) {
supplier.setType(type);
//新增
supplierDao.add(supplier);
}
}
} finally {
if(null != wb) {
try {
wb.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
3、SupplierAction当中
/*
- 导入数据
*/
public void doImport() {
//文件类型判断
if(!“application/vnd.ms-excel”.equals(fileContentType)){
write(ajaxReturn(false, “上传文件必须是excel文件”));
return;
}
try {
supplierBiz.doImport(new FileInputStream(file));
write(ajaxReturn(true, “上传文件成功”));
} catch (ErpException e) {
write(ajaxReturn(false, e.getMessage()));
} catch (IOException e) {
write(ajaxReturn(false, “上传文件失败”));
e.printStackTrace();
}
}
4、供应商或客户的导入——前端
(1)修改supplier.html
(2)修改crud.js
},‘-’,{
text: ‘导入’,
iconCls: ‘icon-save’,
handler: function(){
$(‘#importDlg’).dialog(‘open’);
}
}]
//判断是否有导入的功能
var importForm = document.getElementById(“importForm”);
if(importForm){
//添加的窗口
$(‘#importDlg’).dialog({
title:‘导入数据’,
width:330,
height:160,
modal:true,
closed:true,
buttons:[
{
text: ‘导入’,
handler:function(){
$.ajax({
url: name + ‘_doImport’,
data:new FormData($(‘#importForm’)[0]),
type:‘post’,
processData:false,
contentType:false,
dataType:‘json’,
success:function(rtn){
$.messager.alert(‘提示’,rtn.message,‘info’,function(){
if(rtn.success){
$(‘#importDlg’).dialog(‘close’);
$(‘#importForm’).form(‘clear’);
$(‘#grid’).datagrid(‘reload’);
}
});
}
});
}
}
]
});
}