java解决excel输出为竖向的方法

使用easyExcel的时候,没找到将excel输出为竖向的方式,特此记下excel输出为竖向的方法
注释类

import java.lang.annotation.*;

@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface GetName {
    String name() ;
}

controller层

 public void downloadById(String id, HttpServletResponse response, HttpServletRequest request) throws IOException {
        String fileName = "name" + DateUtil.dateToStr(new Date(), SystemConstant.FORMATTER_M);
        String userAgent = request.getHeader(ExportCode.USERAGENT);
        // 解决火狐导出文件名乱码问题
        try {
            if (StringUtils.contains(userAgent, ExportCode.FIREFOX)) {
                fileName = new String(fileName.getBytes(ExportCode.EXPORTCHARACTER), ExportCode.FILENAMECHARACTER);
            } else {
                fileName = URLEncoder.encode(fileName, ExportCode.EXPORTCHARACTER);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setCharacterEncoding(ExportCode.EXPORTCHARACTER);
        // 设置contentType为excel格式
        response.setContentType(ExportCode.CONTENTTYPE);
        response.setHeader(ExportCode.CONTENT_DIS, ExportCode.HEADFILENAME + fileName + ExportCode.EXCELFORMAT);
        Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿
        Sheet sheet = wb.createSheet("sheet1");  // 创建第一个Sheet页
        sheet.setColumnWidth(1, 31 * 256);//设置第二列的宽度是31个字符宽度
        sheet.setColumnWidth(0, 31 * 256);
        TStatistics statistics = statisticsService.downloadById(id);
        String json = statistics.getJson();
        StatisticsDto statisticsPageDto = JSONObject.parseObject(json, StatisticsDto.class);
        //得到类的反射对象
        Field[] list = statisticsPageDto.getClass().getDeclaredFields();
        for (int i = 0; i < list.length; i++) {
            Field field = list[i];
            if (field.isAnnotationPresent(GetName.class)) {
                field.setAccessible(true);
                GetName annotation = field.getAnnotation(GetName.class);
                String title = annotation.name();
                String value = "";
                try {
                    value = field.get(statisticsPageDto).toString();
                } catch (Exception ignored) {
                }
                Row row = sheet.createRow(i); // 创建一行
                //此处的要求只是一列标题,另一列为值
                //标题1   value1
                //标题2   value2
                //标题3   value3
                row.createCell(0).setCellValue(title);
                row.createCell(1).setCellValue(value);
                field.setAccessible(false);
            }
        }
        ServletOutputStream fileOut = null;
        try {
            fileOut = response.getOutputStream();
            wb.write(fileOut);
            fileOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 可以使用Apache POI库,可以使用以下代码将Excel文件转换为PDF:XWPFDocument document = new XWPFDocument();FileOutputStream out = new FileOutputStream(new File("result.pdf"));PdfOptions options = PdfOptions.create();PdfConverter.getInstance().convert(document, out, options); ### 回答2: 要实现将Excel文件转化为PDF并输出,可以使用Java的Apache POI和iText库。 首先,需要在项目中引入Apache POI和iText的相关依赖。在Maven项目中,可以在pom.xml文件中添加以下依赖: ```xml <dependencies> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13</version> </dependency> </dependencies> ``` 然后,使用Apache POI读取Excel文件,并将内容输出到PDF中。以下是具体代码示例: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import com.itextpdf.text.Document; import com.itextpdf.text.PageSize; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class ExcelToPdfConverter { public static void main(String[] args) { try { // 读取Excel文件 FileInputStream input = new FileInputStream("input.xlsx"); Workbook workbook = new XSSFWorkbook(input); Sheet sheet = workbook.getSheetAt(0); // 创建PDF文档 Document document = new Document(PageSize.A4); PdfWriter.getInstance(document, new FileOutputStream("output.pdf")); document.open(); // 遍历Excel表格,将内容写入PDF for (Row row : sheet) { for (Cell cell : row) { String value = cell.getStringCellValue(); // 获取单元格的值 document.add(new com.itextpdf.text.Paragraph(value)); } } // 关闭PDF文档 document.close(); workbook.close(); System.out.println("Excel转化为PDF成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 以上代码示例中,假设要转换的Excel文件名为input.xlsx,将转换后的PDF文件输出为output.pdf。使用`FileInputStream`读取Excel文件,`PdfWriter`创建PDF文档,再通过遍历Excel表格将内容写入PDF。 最后,通过运行该Java程序,即可将Excel文件转化为PDF并输出到指定位置。 ### 回答3: 要实现将Excel文件转化为PDF输出,可以使用Java中的POI和iText库来实现。以下是实现的具体代码: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import com.itextpdf.text.Document; import com.itextpdf.text.Element; import com.itextpdf.text.PageSize; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class ExcelToPdfConverter { public static void main(String[] args) { try { // 读取Excel文件 FileInputStream excelFile = new FileInputStream("input.xlsx"); Workbook workbook = new XSSFWorkbook(excelFile); Sheet sheet = workbook.getSheetAt(0); // 创建PDF文件 Document document = new Document(PageSize.A4); FileOutputStream pdfFile = new FileOutputStream("output.pdf"); PdfWriter.getInstance(document, pdfFile); document.open(); // 遍历Excel表格中的行和列 for (Row row : sheet) { for (Cell cell : row) { // 创建段落,并将Excel单元格内容添加到段落中 Paragraph paragraph = new Paragraph(cell.toString()); paragraph.setAlignment(Element.ALIGN_LEFT); document.add(paragraph); } } // 关闭PDF文件和Excel文件 document.close(); pdfFile.close(); workbook.close(); excelFile.close(); System.out.println("Excel转化为PDF成功!"); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } ``` 该代码中,首先使用POI库读取Excel文件,然后使用iText库创建PDF文件。接着使用循环遍历Excel表格中的行和列,将每个单元格的内容添加到PDF文件中。最后关闭PDF文件和Excel文件,并输出成功信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值