package tool;
import com.cqie.javacs.dao.Dao;
import com.cqie.javacs.entity.Goods;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javax.print.*;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.JobName;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
public class PrintExcel1 {
private PrintExcel1() {}
public static void Print() throws IOException, PrintException {
// 示例数据
Vector<Goods> allGoods = new Dao().getAllGoods();
List<String[]> goods = new ArrayList<String[]>();
goods.add(new String[]{"商品编号","商品名称","商品类别","库存数量","销售数量","售价","生产日期","保质期"});
for (Goods allGood : allGoods) {
goods.add(new String[]{
allGood.getId(),
allGood.getName(),
allGood.getCategory(),
String.valueOf(allGood.getCount()),
String.valueOf(allGood.getSellcount()),
String.valueOf(allGood.getPrice()),
allGood.getDataOfManufacture(),
String.valueOf(allGood.getExpiration()),
});
}
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("商品表");
for (int i = 0; i < goods.size(); i++) {
Row row = sheet.createRow(i);
for (int j = 0; j < goods.get(i).length; j++) {
Cell cell = row.createCell(j);
cell.setCellValue(goods.get(i)[j]);
}
}
try (
FileOutputStream outputStream = new FileOutputStream("商品库存.xlsx")) {
workbook.write(outputStream);
} finally {
workbook.close();
}
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
PrintRequestAttributeSet attributeSet = new HashPrintRequestAttributeSet();
attributeSet.add(new JobName("Print",null));
attributeSet.add(new Copies(1));
DocFlavor docFlavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
DocPrintJob printJob = printService.createPrintJob();
FileInputStream inputStream = new FileInputStream("D:\\DemoJavaApplication\\商品库存.xlsx");
Doc doc = new SimpleDoc(inputStream, docFlavor, null);
printJob.print(doc,attributeSet);
inputStream.close();
workbook.close();
}
}

为什么会出现这个问题,求解答