windchill客制化导入部件到Excel

JAVA后端代码

package ext.corilead.report;


import java.io.File;
import java.io.FileInputStream;

import java.io.FileNotFoundException;
import java.io.IOException;

import java.io.Serializable;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
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.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import wt.fc.Persistable;
import wt.fc.ReferenceFactory;


import wt.fc.WTReference;
import wt.method.RemoteAccess;

import wt.part.WTPart;
import wt.util.WTException;
import wt.util.WTProperties;



/**
 * @author ZhangBing
 * @QQ: 1530574429
 * @date 2022/1/14
 */
public class ExportPartDetailReports implements RemoteAccess, Serializable {

  private static String wtHome = "";
  private static WTProperties wtProperties;

  static {
    try {
      wtProperties = WTProperties.getLocalProperties();
      wtHome = wtProperties.getProperty("wt.home");
    } catch (IOException e) {
      e.getMessage();
    }
  }

  public static XSSFWorkbook buildExcel (String oid) throws WTException, IOException {
//Excel的标题
      String[] tytle = {"部件编号", "部件名称", "生命周期", "库名称", "创建者"};
      //通过oid获取部件对象
      WTReference partRef = new ReferenceFactory().getReference(oid);
      
      WTPart  part = (WTPart) partRef.getObject();
//通过方法获取模板地址
      String filePath = getTemplate();
      //通过模板创建Excel
      XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(filePath));
  //    创建工作表
      XSSFSheet sheet = wb.createSheet("sheet1");
      //创建第一行
      XSSFRow row = sheet.createRow(0);
      //单元格格式居中
      XSSFCellStyle cellStyle = wb.createCellStyle();
      cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
      //先创建列为0
      XSSFCell cell = null;
     //通过对象获取数据
      String[] values = {part.getNumber(), part.getName(), part.getLifeCycleState().getDisplay(),
          part.getContainer().getName(), part.getCreatorFullName()};

        //创建标题
        for (int i = 0; i < tytle.length; i++) {
          cell = row.createCell(i);
          cell.setCellValue(tytle[i]);
          cell.setCellStyle(cellStyle);
        }
        //把获取到的数据遍历进入第二行
        row = sheet.createRow(1);
        for (int i = 0; i < tytle.length; i++) {
          row.createCell(i).setCellValue(values[i]);
        }
//将导入数据的Excel表模板返回到jsp页面
      return wb;
  }
 /* public static HSSFWorkbook buildExcel() throws IOException {
    String[] tytle = {"部件编号", "部件名称", "生命周期", "库名称", "创建者"};
    String filePath = getTemplate();
    HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(filePath));
    HSSFSheet sheet = wb.createSheet("sheet1");
    HSSFRow row = sheet.createRow(0);
    HSSFCellStyle cellStyle = wb.createCellStyle();
    cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    HSSFCell cell = null;
    //创建标题
    for (int i = 0; i < tytle.length; i++) {
      cell = row.createCell(i);
      cell.setCellValue(tytle[i]);
      cell.setCellStyle(cellStyle);
    }
    return wb;
  }*/

  private static String getTemplate() {
    String filePath;
    //这里是Excel表的模板地址
    filePath = wtHome + File.separator + "codebase" + File.separator + "templates" + File.separator + "导出的表.xlsx";
    return filePath;
  }
}

jsp代码

<%@ page contentType="text/html; charset=UTF-8" language="java"
         import="java.util.*,
	wt.part.WTPart,ext.corilead.report.*,wt.fc.*,java.io.*,org.apache.poi.hssf.usermodel.*"
         pageEncoding="UTF-8" autoFlush="true" buffer="3000kb"%>
<%@ page import="org.apache.poi.xssf.usermodel.XSSFWorkbook" %>

<%@include file="/netmarkets/jsp/components/beginWizard.jspf"%>

<%
    //获取oid
    String oid =request.getParameter("oid");
    //调用上面的方法,得到导入数据的模板
    XSSFWorkbook wb = ExportPartDetailReports.buildExcel(oid);
    String fileName = "导出的表.xlsx";
    fileName =new String(fileName.getBytes(),"ISO-8859-1");
    //响应流的设置
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    response.addHeader("Content-Disposition", "attachment;filename="+ fileName );
    response.addHeader("Pargam", "no-cache");
    response.addHeader("Cache-Control", "no-cache");
//通过流的方式创建Excel
    try {
        OutputStream os = response.getOutputStream();
        wb.write(os);
        os.flush();
        os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
%>



<%@include file="/netmarkets/jsp/util/end.jspf"%>
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值