• 第一次写MVC 程序 SpringBoot Web App


    这次主要学会了怎样在控制器Controller 和 视图 Views之前传递数据,感谢帮助过我的人

    这次用Thymeleaf 和Bootstrap都失败了,Angular太复杂,完全不会怎样用,因为本身Angular是MVC,Java也用MVC架构我实在搞不清要怎么弄

    Thymeleaf使用后会整个网站有莫名奇妙的怪事而没用

    Bootstrap在浏览器F12后进入Console模式后没有Jquery或者什么的不太正常

    版本 JDK 8 SpringBoot 2.x

    在Maven里加下面依赖

    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>
     <dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jasper</artifactId>
    <version>9.0.36</version>
    </dependency>

    <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.15</version>
    </dependency>

    <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.15</version>
    </dependency>

    <dependency>
    <groupId>com.thoughtworks.xstream</groupId>
    <artifactId>xstream</artifactId>
    <version>1.4.8</version>
    </dependency>

    <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.9</version>
    </dependency>

    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    </dependency>


    </dependencies>

    SpringBoot的配置文件

    spring.servlet.multipart.max-file-size=100MB
    spring.servlet.multipart.max-request-size=100MB
    spring.mvc.view.suffix=.jsp
    spring.mvc.view.prefix=/

    控制器 HomeController

    @Controller
    public class HomeController {

    @RequestMapping( value={"/","/home"})
    public String home(){

    System.out.print("hi");
    return "home";
    }




    @PostMapping("/uploadfile")
    public ModelAndView uploadFile(@RequestParam("file") MultipartFile file,
    @RequestParam(name="address", required = false) String myaddress,
    @RequestParam(name="bankaccount", required = false) String mybankaccount,
    @RequestParam(name="kpr", required = false) String mykpr,
    @RequestParam(name="skr", required = false) String myskr,
    @RequestParam(name="fhr", required = false) String myfhr) {

    String TYPE = "application/vnd.ms-excel";

    if (TYPE.equals(file.getContentType())) {
    try {


    List<ExcelContents> tempExcelcontentList = new ArrayList<ExcelContents>();
    HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());


    String consequencemsg="";
    int errsheetcount=0;
    int recordscount=0;
    int sheetscount=0;
    int invoiceindex=1;

    for (int j= 0; j < workbook.getNumberOfSheets(); j++) {
    HSSFSheet worksheet = workbook.getSheetAt(j);
    String sheetname=workbook.getSheetName(j);
    short notoligable=0;
    for(int i=0;i<worksheet.getPhysicalNumberOfRows() ;i++) {
    HSSFRow row=worksheet.getRow(i);
    if (i==0)
    {

    String title=row.getCell(0).getStringCellValue();
    String comparestr="序号";
    if (!title.trim().equals(comparestr))
    {

    consequencemsg=consequencemsg+"Excel 表名叫"+sheetname+"第"+Integer.toString(i+1) +"个标题不是"+comparestr+",与预定的Excel不同,程序退出 </br>";
    errsheetcount++;
    notoligable=1;
    break;
    }

    title=row.getCell(1).getStringCellValue();
    comparestr="发票代码";
    if (!title.trim().equals(comparestr))
    {
    consequencemsg=consequencemsg+"Excel 表名叫"+sheetname+"第"+Integer.toString(i+1) +"个标题不是"+comparestr+",与预定的Excel不同,程序退出</br> ";
    errsheetcount++;
    notoligable=1;
    break;
    }

    title=row.getCell(2).getStringCellValue();
    comparestr="发票号码";
    if (!title.trim().equals(comparestr))
    {
    consequencemsg=consequencemsg+"Excel 表名叫"+sheetname+"第"+Integer.toString(i+1) +"个标题不是"+comparestr+",与预定的Excel不同,程序退出 </br>";
    errsheetcount++;
    notoligable=1;
    break;
    }

    title=row.getCell(3).getStringCellValue();
    comparestr="销方识别号";
    if (!title.trim().equals(comparestr))
    {
    consequencemsg=consequencemsg+"Excel 表名叫"+sheetname+"第"+Integer.toString(i+1) +"个标题不是"+comparestr+",与预定的Excel不同,程序退出 </br>";
    errsheetcount++;
    notoligable=1;
    break;
    }

    title=row.getCell(4).getStringCellValue();
    comparestr="销方名称";
    if (!title.trim().equals(comparestr))
    {
    consequencemsg=consequencemsg+"Excel 表名叫"+sheetname+"第"+Integer.toString(i+1) +"个标题不是"+comparestr+",与预定的Excel不同,程序退出</br> ";
    errsheetcount++;
    notoligable=1;
    break;
    }

    title=row.getCell(5).getStringCellValue();
    comparestr="购方识别号";
    if (!title.trim().equals(comparestr))
    {
    consequencemsg=consequencemsg+"Excel 表名叫"+sheetname+"第"+Integer.toString(i+1) +"个标题不是"+comparestr+",与预定的Excel不同,程序退出 </br>";
    errsheetcount++;
    notoligable=1;
    break;
    }

    title=row.getCell(6).getStringCellValue();
    comparestr="购方名称";
    if (!title.trim().equals(comparestr))
    {
    consequencemsg=consequencemsg+"Excel 表名叫"+sheetname+"第"+Integer.toString(i+1) +"个标题不是"+comparestr+",与预定的Excel不同,程序退出 </br>";
    errsheetcount++;
    notoligable=1;
    break;
    }


    title=row.getCell(7).getStringCellValue();
    comparestr="购方地址电话";
    if (!title.trim().equals(comparestr))

    {
    consequencemsg=consequencemsg+"Excel 表名叫"+sheetname+"第"+Integer.toString(i+1) +"个标题不是"+comparestr+",与预定的Excel不同,程序退出 </br>";
    errsheetcount++;
    notoligable=1;
    break;
    }

    title=row.getCell(8).getStringCellValue();
    comparestr="购方银行账号";
    if (!title.trim().equals(comparestr))
    {
    consequencemsg=consequencemsg+"Excel 表名叫"+sheetname+"第"+Integer.toString(i+1) +"个标题不是"+comparestr+",与预定的Excel不同,程序退出 </br>";
    errsheetcount++;
    notoligable=1;
    break;
    }

    title=row.getCell(9).getStringCellValue();
    comparestr="密文";
    if (!title.trim().equals(comparestr))
    {
    consequencemsg=consequencemsg+"Excel 表名叫"+sheetname+"第"+Integer.toString(i+1) +"个标题不是"+comparestr+",与预定的Excel不同,程序退出</br> ";
    errsheetcount++;
    notoligable=1;
    break;
    }

    title=row.getCell(10).getStringCellValue();
    comparestr="开票日期";
    if (!title.trim().equals(comparestr))
    {
    consequencemsg=consequencemsg+"Excel 表名叫"+sheetname+"第"+Integer.toString(i+1) +"个标题不是"+comparestr+",与预定的Excel不同,程序退出 </br>";
    errsheetcount++;
    notoligable=1;
    break;
    }

    title=row.getCell(11).getStringCellValue();
    comparestr="金额";
    if (!title.trim().equals(comparestr))
    {
    consequencemsg=consequencemsg+"Excel 表名叫"+sheetname+"第"+Integer.toString(i+1) +"个标题不是"+comparestr+",与预定的Excel不同,程序退出</br> ";
    errsheetcount++;
    notoligable=1;
    break;
    }

    title=row.getCell(12).getStringCellValue();
    comparestr="税额";
    if (!title.trim().equals(comparestr))
    {
    consequencemsg=consequencemsg+"Excel 表名叫"+sheetname+"第"+Integer.toString(i+1) +"个标题不是"+comparestr+",与预定的Excel不同,程序退出 </br>";
    errsheetcount++;
    notoligable=1;
    break;
    }

    title=row.getCell(13).getStringCellValue();
    comparestr="主要商品名称";
    if (!title.trim().equals(comparestr))
    {
    consequencemsg=consequencemsg+"Excel 表名叫"+sheetname+"第"+Integer.toString(i+1) +"个标题不是"+comparestr+",与预定的Excel不同,程序退出 </br>";
    errsheetcount++;
    notoligable=1;
    break;
    }

    title=row.getCell(14).getStringCellValue();
    comparestr="备注";
    if (!title.trim().equals(comparestr))
    {
    consequencemsg=consequencemsg+"Excel 表名叫"+sheetname+"第"+Integer.toString(i+1) +"个标题不是"+comparestr+",与预定的Excel不同,程序退出 </br>";
    errsheetcount++;
    notoligable=1;
    break;
    }

    title=row.getCell(15).getStringCellValue();
    comparestr="是否作废";
    if (!title.trim().equals(comparestr))
    {
    consequencemsg=consequencemsg+"Excel 表名叫"+sheetname+"第"+Integer.toString(i+1) +"个标题不是"+comparestr+",与预定的Excel不同,程序退出 </br>";
    errsheetcount++;
    notoligable=1;
    break;
    }

    title=row.getCell(16).getStringCellValue();
    comparestr="发票类别";
    if (!title.trim().equals(comparestr))
    {
    consequencemsg=consequencemsg+"Excel 表名叫"+sheetname+"第"+Integer.toString(i+1) +"个标题不是"+comparestr+",与预定的Excel不同,程序退出</br> ";
    errsheetcount++;
    notoligable=1;
    break;
    }

    title=row.getCell(17).getStringCellValue();
    comparestr="是否收购";
    if (!title.trim().equals(comparestr))
    {
    consequencemsg=consequencemsg+"Excel 表名叫"+sheetname+"第"+Integer.toString(i+1) +"个标题不是"+comparestr+",与预定的Excel不同,程序退出 </br>";
    errsheetcount++;
    notoligable=1;
    break;
    }

    title=row.getCell(18).getStringCellValue();
    comparestr="是否差额征收";
    if (!title.trim().equals(comparestr))
    {
    consequencemsg=consequencemsg+"Excel 表名叫"+sheetname+"第"+Integer.toString(i+1) +"个标题不是"+comparestr+",与预定的Excel不同,程序退出 </br>";
    errsheetcount++;
    notoligable=1;
    break;
    }

    title=row.getCell(19).getStringCellValue();
    comparestr="开票机号";
    if (!title.trim().equals(comparestr))
    {
    consequencemsg=consequencemsg+"Excel 表名叫"+sheetname+"第"+Integer.toString(i+1) +"个标题不是"+comparestr+",与预定的Excel不同,程序退出 </br>";
    errsheetcount++;
    notoligable=1;
    break;
    }

    title=row.getCell(20).getStringCellValue();
    comparestr="商品名称";
    if (!title.trim().equals(comparestr))
    {
    consequencemsg=consequencemsg+"Excel 表名叫"+sheetname+"第"+Integer.toString(i+1) +"个标题不是"+comparestr+",与预定的Excel不同,程序退出 </br>";
    errsheetcount++;
    notoligable=1;
    break;
    }

    title=row.getCell(21).getStringCellValue();
    comparestr="商品税收分类编码";
    if (!title.trim().equals(comparestr))
    {
    consequencemsg=consequencemsg+"Excel 表名叫"+sheetname+"第"+Integer.toString(i+1) +"个标题不是"+comparestr+",与预定的Excel不同,程序退出</br> ";
    errsheetcount++;
    notoligable=1;
    break;
    }

    title=row.getCell(22).getStringCellValue();
    comparestr="数量";
    if (!title.trim().equals(comparestr))
    {
    consequencemsg=consequencemsg+"Excel 表名叫"+sheetname+"第"+Integer.toString(i+1) +"个标题不是"+comparestr+",与预定的Excel不同,程序退出 </br>";
    errsheetcount++;
    notoligable=1;
    break;
    }

    title=row.getCell(23).getStringCellValue();
    comparestr="金额";
    if (!title.trim().equals(comparestr))
    {
    consequencemsg=consequencemsg+"Excel 表名叫"+sheetname+"第"+Integer.toString(i+1) +"个标题不是"+comparestr+",与预定的Excel不同,程序退出</br> ";
    errsheetcount++;
    notoligable=1;
    break;
    }

    title=row.getCell(24).getStringCellValue();
    comparestr="税额";
    if (!title.trim().equals(comparestr))
    {
    consequencemsg=consequencemsg+"Excel 表名叫"+sheetname+"第"+Integer.toString(i+1) +"个标题不是"+comparestr+",与预定的Excel不同,程序退出</br> ";
    errsheetcount++;
    notoligable=1;
    break;
    }

    title=row.getCell(25).getStringCellValue();
    comparestr="税率";
    if (!title.trim().equals(comparestr))
    {
    consequencemsg=consequencemsg+"Excel 表名叫"+sheetname+"第"+Integer.toString(i+1) +"个标题不是"+comparestr+",与预定的Excel不同,程序退出 </br>";
    errsheetcount++;
    notoligable=1;
    break;
    }

    title=row.getCell(26).getStringCellValue();
    comparestr="规格型号";
    if (!title.trim().equals(comparestr))
    {
    consequencemsg=consequencemsg+"Excel 表名叫"+sheetname+"第"+Integer.toString(i+1) +"个标题不是"+comparestr+",与预定的Excel不同,程序退出 </br>";
    errsheetcount++;
    notoligable=1;
    break;
    }

    }
    else
    {
    ExcelContents invoiceexcelcontent = new ExcelContents();
    invoiceexcelcontent.setSn(row.getCell(0).getStringCellValue());
    invoiceexcelcontent.setInvoicecode(row.getCell(1).getStringCellValue());
    invoiceexcelcontent.setInvoicenum(row.getCell(2).getStringCellValue());
    invoiceexcelcontent.setSellerid(row.getCell(3).getStringCellValue());
    invoiceexcelcontent.setSellername(row.getCell(4).getStringCellValue());
    invoiceexcelcontent.setBuyerid(row.getCell(5).getStringCellValue());
    invoiceexcelcontent.setBuyername(row.getCell(6).getStringCellValue());
    invoiceexcelcontent.setBuyeradd(row.getCell(7).getStringCellValue());
    invoiceexcelcontent.setBuyerbankacc(row.getCell(8).getStringCellValue());
    invoiceexcelcontent.setCryptocontents(row.getCell(9).getStringCellValue());
    invoiceexcelcontent.setIssuedate(row.getCell(10).getStringCellValue());
    invoiceexcelcontent.setTotal(row.getCell(11).getNumericCellValue());
    invoiceexcelcontent.setTaxtotal(row.getCell(12).getNumericCellValue());
    invoiceexcelcontent.setMainproductname(row.getCell(13).getStringCellValue());
    invoiceexcelcontent.setMemo(row.getCell(14).getStringCellValue());
    invoiceexcelcontent.setIfappeal(row.getCell(15).getStringCellValue());
    invoiceexcelcontent.setInvoicetype(row.getCell(16).getStringCellValue());
    invoiceexcelcontent.setIfaccquired(row.getCell(17).getStringCellValue());
    invoiceexcelcontent.setIfdiffecent(row.getCell(18).getStringCellValue());
    invoiceexcelcontent.setPoscode(row.getCell(19).getStringCellValue());
    invoiceexcelcontent.setProductname(row.getCell(20).getStringCellValue());
    invoiceexcelcontent.setTaxtypecode(row.getCell(21).getStringCellValue());

    if (isNumeric(row.getCell(22).getStringCellValue().trim()))
    {
    invoiceexcelcontent.setThenumber(Double.parseDouble(row.getCell(22).getStringCellValue().trim()));

    }
    else
    {
    invoiceexcelcontent.setThenumber(0);
    consequencemsg=consequencemsg+"警告: 在Excel表名叫'"+sheetname+"' 第"+ Integer.toString(i+1) +"行'数量'字段不是字符类型或者没有数值而转换出现问题,已经当作数量为零处理</br>";
    }

    // invoiceexcelcontent.setThenumber(row.getCell(22).getNumericCellValue());
    invoiceexcelcontent.setAmount(row.getCell(23).getNumericCellValue());
    invoiceexcelcontent.setTax(row.getCell(24).getNumericCellValue());
    invoiceexcelcontent.setTaxrate(row.getCell(25).getNumericCellValue());
    invoiceexcelcontent.setProdsize(row.getCell(26).getStringCellValue());
    tempExcelcontentList.add(invoiceexcelcontent);
    recordscount++;
    }


    }

    if(notoligable==0)
    {
    consequencemsg=consequencemsg+"Excel 的'"+sheetname+"'导入成功 </br>";
    sheetscount++;
    }
    }


    if (errsheetcount==workbook.getNumberOfSheets())
    {
    consequencemsg="些excel不是增值税发票Excel,不能导入任何数据</br>";
    }
    else if(recordscount==0)
    {
    consequencemsg="些excel数据为空</br>";
    }
    else
    {
    consequencemsg=consequencemsg+" 共导入"+Integer.toString(sheetscount)+"个表,"+Integer.toString(recordscount)+"个记录</br>";
    }

    workbook.close();

    String invoicenumbercurrent="";
    String invoicenumberprevious="";
    DZSBB dzsbb=new DZSBB();
    SB_FWSKKP fwskpp=new SB_FWSKKP();
    dzsbb.setSB_FWSKKP(fwskpp);
    SB_FWSKKP_BTXX fwskkpbtxx=new SB_FWSKKP_BTXX();
    SB_FWSKKP_FPXX fwskkpfpxx=new SB_FWSKKP_FPXX();
    List<SB_FWSKKP_MXXX> mxxlist=new ArrayList<SB_FWSKKP_MXXX>();
    List<SP> splist=new ArrayList<SP>();
    fwskpp.setSB_FWSKKP_FPXX(fwskkpfpxx);
    fwskkpfpxx.setSB_FWSKKP_MXXX(mxxlist);

    int elementindex=0;
    for (ExcelContents xc:tempExcelcontentList) {

    if (elementindex==0)
    {
    fwskkpbtxx.setSB_FWSKKP_BTXX_NSRSBH(xc.getSellerid());
    String issuedatestring=xc.getIssuedate();
    if (issuedatestring.length()>=6)
    fwskkpbtxx.setSB_FWSKKP_BTXX_KPYF(issuedatestring.substring(6));

    }


    invoicenumbercurrent=xc.getInvoicenum();



    SB_FWSKKP_MXXX fwskkpmxxx=new SB_FWSKKP_MXXX();
    fwskkpmxxx.setSB_FWSKKP_MXXX_XH(Integer.toString(invoiceindex));
    fwskkpmxxx.setSB_FWSKKP_MXXX_FPDM(xc.getInvoicecode());
    fwskkpmxxx.setSB_FWSKKP_MXXX_FPHM(xc.getInvoicenum());

    MXXX mxxx=new MXXX();
    mxxx.setSB_FWSKKP_MXXX_XFDZDH(myaddress);
    mxxx.setSB_FWSKKP_MXXX_XFYHZH(mybankaccount);
    fwskkpmxxx.setMXXX(mxxx);
    fwskkpmxxx.setSB_FWSKKP_MXXX_XFMC(xc.getSellername());
    fwskkpmxxx.setSB_FWSKKP_MXXX_XFSBH(xc.getSellerid());
    fwskkpmxxx.setSB_FWSKKP_MXXX_GFMC(xc.getBuyername());
    fwskkpmxxx.setSB_FWSKKP_MXXX_GFSBH(xc.getBuyerid());
    fwskkpmxxx.setSB_FWSKKP_MXXX_KPJH(xc.getPoscode());
    fwskkpmxxx.setSB_FWSKKP_MXXX_GFDZDH(xc.getBuyeradd());
    fwskkpmxxx.setSB_FWSKKP_MXXX_GFYHZH(xc.getBuyerbankacc());
    fwskkpmxxx.setSB_FWSKKP_MXXX_MW(xc.getCryptocontents());
    fwskkpmxxx.setSB_FWSKKP_MXXX_KPRQ(xc.getIssuedate());
    fwskkpmxxx.setSB_FWSKKP_MXXX_JE(xc.getTotal());
    fwskkpmxxx.setSB_FWSKKP_MXXX_SL(xc.getTaxrate());
    fwskkpmxxx.setSB_FWSKKP_MXXX_SE(xc.getTaxtotal());
    fwskkpmxxx.setSB_FWSKKP_MXXX_ZYSPMC(xc.getMainproductname());
    String invoicetype=xc.getInvoicetype();
    if (invoicetype.trim().equals("增值税普通发票"))
    {
    fwskkpmxxx.setSB_FWSKKP_MXXX_SPSM("4001");
    fwskkpmxxx.setSB_FWSKKP_MXXX_FPZL(2);
    }
    else
    {
    fwskkpmxxx.setSB_FWSKKP_MXXX_FPZL(1);
    }

    fwskkpmxxx.setSB_FWSKKP_MXXX_BZ(xc.getMemo());


    String ifdifference=xc.getIfdiffecent();
    if (ifdifference.trim().equals("非差额发票"))
    fwskkpmxxx.setSB_FWSKKP_MXXX_CEZS(0);
    else
    fwskkpmxxx.setSB_FWSKKP_MXXX_CEZS(1);

    String ifappealstr=xc.getIfappeal();

    if(ifappealstr.trim().equals("未作废"))
    fwskkpmxxx.setSB_FWSKKP_MXXX_ZFBZ(0);
    else
    fwskkpmxxx.setSB_FWSKKP_MXXX_ZFBZ(1);

    String ifaccquired=xc.getIfaccquired();
    if(ifaccquired.trim().equals("非收购发票"))
    fwskkpmxxx.setSB_FWSKKP_MXXX_SGBZ(0);
    else
    fwskkpmxxx.setSB_FWSKKP_MXXX_SGBZ(1);

    fwskkpmxxx.setSB_FWSKKP_MXXX_JSHJ(xc.getTotal()+xc.getTaxtotal());
    fwskkpmxxx.setSB_FWSKKP_MXXX_KPR(mykpr);
    fwskkpmxxx.setSB_FWSKKP_MXXX_SKR(myskr);
    fwskkpmxxx.setSB_FWSKKP_MXXX_FHR(myfhr);


    if (!invoicenumbercurrent.equals(invoicenumberprevious))
    {

    mxxlist.add(fwskkpmxxx);
    splist.clear();
    }


    SP sp=new SP();
    sp.setJE(xc.getAmount());
    sp.setSL(xc.getTaxrate());
    sp.setSE(xc.getTax());
    sp.setSPMC(xc.getProductname());
    sp.setGGXH(xc.getProdsize());
    sp.setJLDW("盒");
    sp.setSHUL(xc.getThenumber());
    sp.setDJ((xc.getAmount()+xc.getTax())/xc.getThenumber());
    sp.setFLBM(xc.getInvoicecode());
    sp.setHSBZ("True");
    splist.add(sp);



    invoiceindex++;

    List<SP> mysplist=new ArrayList<SP>();
    mysplist.addAll(splist);
    fwskkpmxxx.setSPXX(mysplist);



    elementindex++;
    invoicenumberprevious=invoicenumbercurrent;
    }

    UUID uuid = UUID.randomUUID();

    File xmlfilename = new File(uuid.toString()+".xml");

    JAXBContext jaxbContext = JAXBContext.newInstance(DZSBB.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    // output pretty printed
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    jaxbMarshaller.marshal(dzsbb, xmlfilename);
    jaxbMarshaller.marshal(dzsbb, System.out);

    ModelAndView model = new ModelAndView("result");
    model.addObject("excecontentslist",tempExcelcontentList);
    model.addObject("message",consequencemsg);
    model.addObject("downloadfile",xmlfilename);
    model.addObject("test","hello world!");
    return model;

    } catch (Exception e) {
    e.printStackTrace();
    throw new FileStorageException("读取Excel时出错, " + file.getOriginalFilename()
    + ". 请重试!");
    }
    }
    else
    {
    ModelAndView model = new ModelAndView("exception");

    model.addObject("message","此文件不是Excel");

    return model;
    }
    }


    @RequestMapping(path = "/download", method = RequestMethod.POST)
    public void saveTxtFile(HttpServletResponse response,@RequestParam("filename") String filename) throws IOException {

    String yourXmlFileInAString;
    response.setContentType("application/xml;charset=UTF-8");
    response.setCharacterEncoding("UTF-8");
    response.setHeader("Content-Disposition", "attachment;filename="+filename);

    BufferedReader br = new BufferedReader(new FileReader(filename));
    String line;
    StringBuilder sb = new StringBuilder();

    while((line=br.readLine())!= null){
    sb.append(line);
    }

    yourXmlFileInAString = sb.toString();

    ServletOutputStream outStream = response.getOutputStream();
    outStream.write(yourXmlFileInAString.getBytes(Charset.forName("UTF-8")));
    outStream.flush();
    outStream.close();
    }

    public static boolean isNumeric(String strNum) {
    if (strNum == null) {
    return false;
    }
    try {
    double d = Double.parseDouble(strNum);
    } catch (NumberFormatException nfe) {
    return false;
    }
    return true;
    }
    }

    视图1

    home.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>

    <meta charset="UTF-8">
    <title>增值税发票Excel转换为XML程序(测试版本)</title>
    </head>
    <body>

    <h1>增值税发票Excel转换为XML程序</h1>
    <hr/>
    <h4>上传Excel文件:</h4>
    <form method="POST" action="uploadfile" th:action="@{/uploadfile}" enctype="multipart/form-data">
    <input type="file" name="file" accept=".xls"/> <br/><br/>
    <p>填写补充资料:</p>
    <label for="fname">销方地址:</label>
    <input type="text" id="address" name="address" size="50">
    <label for="lname">销方银行帐号:</label>
    <input type="text" id="bankaccount" name="bankaccount" size="50"><br/><br/>
    <label for="lname">开票员:</label>
    <input type="text" id="kpr" name="kpr" size="10">
    <label for="lname">收款人:</label>
    <input type="text" id="skr" name="skr" size="10">
    <label for="lname">复核人:</label>
    <input type="text" id="fhr" name="fhr" size="10"><br/><br/>
    <button type="submit">提交</button>
    </form>
    <hr/>
    <div th:if="${message}">
    <h2 th:text="${message}"></h2>
    </div>

    <div>

    </div>



    </body>


    </html>

    localhost:8080/home</br>
    注意:上传的文件限制最大为100M</br>
    由于单位字段Excel是没有些数据的,只能固定用 盒

    视图2

    result.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <!DOCTYPE html>
    <html>
    <head>

    <meta charset="UTF-8">
    <title>结果</title>
    </head>
    <body>



    <form action="download" method="post">
    <input type="text" id="filename" name="filename" type="text" value="${downloadfile}" readonly/><br/>
    <input type="submit" value="下载XML" />
    </form>

    <p> 信息提示:</p>
    ${message}
    </br>

    <p >Excel内容:</p>
    <table class="table table-hover">

    <thead>
    <tr>
    <th><b>序列</b></th>
    <th><b>发票代码 </b></th>
    <th><b>发票号码</b></th>
    <th><b>销方识别号</b></th>
    <th><b>销方名称</b></th>
    <th><b>购方识别号 </b></th>
    <th><b>购方名称</b></th>
    <th><b>购方地址电话</b></th>
    <th><b>购方银行账号</b></th>
    <th><b>密文</b></th>
    <th><b>开票日期</b></th>
    <th><b>金额</b></th>
    <th><b>税额</b></th>
    <th><b>主要商品名称</b></th>
    <th><b>备注</b></th>
    <th><b>是否作废</b></th>
    <th><b>发票类别</b></th>
    <th><b>是否收购</b></th>
    <th><b>是否差额征收</b></th>
    <th><b>开票机号</b></th>
    <th><b>商品名称</b></th>
    <th><b>发票类别</b></th>
    <th><b>商品税收分类编码</b></th>
    <th><b>数量</b></th>
    <th><b>金额</b></th>
    <th><b>税额</b></th>
    <th><b>税率</b></th>
    <th><b>规格型号</b></th>
    </tr>
    </thead>
    <tbody>

    <c:forEach var="excel" items="${excecontentslist}" varStatus="status">
    <tr>


    <td><c:out value="${status.index + 1}"></c:out></td>
    <td><c:out value="${excel.invoicecode}"></c:out></td>
    <td><c:out value="${excel.invoicenum}"></c:out></td>
    <td><c:out value="${excel.sellerid}"></c:out></td>
    <td><c:out value="${excel.sellername}"></c:out></td>
    <td><c:out value="${excel.buyerid}"></c:out></td>
    <td><c:out value="${excel.buyername}"></c:out></td>
    <td><c:out value="${excel.buyeradd}"></c:out></td>
    <td><c:out value="${excel.buyerbankacc}"></c:out></td>
    <td><c:out value="${excel.cryptocontents}"></c:out></td>
    <td><c:out value="${excel.issuedate}"></c:out></td>
    <td><c:out value="${excel.total}"></c:out></td>
    <td><c:out value="${excel.taxtotal}"></c:out></td>
    <td><c:out value="${excel.mainproductname}"></c:out></td>
    <td><c:out value="${excel.memo}"></c:out></td>
    <td><c:out value="${excel.ifappeal}"></c:out></td>
    <td><c:out value="${excel.invoicetype}"></c:out></td>
    <td><c:out value="${excel.ifaccquired}"></c:out></td>
    <td><c:out value="${excel.ifdiffecent}"></c:out></td>
    <td><c:out value="${excel.poscode}"></c:out></td>
    <td><c:out value="${excel.productname}"></c:out></td>
    <td><c:out value="${excel.taxtypecode}"></c:out></td>
    <td><c:out value="${excel.thenumber}"></c:out></td>
    <td><c:out value="${excel.amount}"></c:out></td>
    <td><c:out value="${excel.tax}"></c:out></td>
    <td><c:out value="${excel.taxrate}"></c:out></td>
    <td><c:out value="${excel.prodsize}"></c:out></td>
    </tr>
    </c:forEach>
    </tbody>
    </table>

    </body>
    </html>

  • 相关阅读:
    异常作业
    多态作业
    封装和继承作业
    类和对象作业
    多重循环、方法作业
    选择语句+循环语句作业
    数据类型和运算符作业
    初识Java作业
    C 数据结构堆
    C基础 旋转数组查找题目
  • 原文地址:https://www.cnblogs.com/redmondfan/p/13570341.html
Copyright © 2020-2023  润新知