菜单

java poi读取excel操作示例(贰个代码)

2019年2月16日 - www6165com

3:抓取字段显示结果不完全:。

java读取各档次的公文

用到的多少个包

bcmail-jdk14-132.jar/bcprov-jdk14-132.jar/checkstyle-all-4.2.jar/FontBox-0.1.0-dev.jar/lucene-core-2.0.0.jar/PDFBox-0.7.3.jar/poi-3.0-alpha3-20061212.jar/poi-contrib-3.0-alpha3-20061212.jar/poi-scratchpad-3.0-alpha3-20061212.jar

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.hssf.usermodel.HSSFCell;
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.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.pdfbox.pdfparser.PDFParser;
import org.pdfbox.util.PDFTextStripper;

public class Test {

/**
* @param args
*/
public static void p(Object obj) {
   System.out.println(obj);
}

public static void main(String[] args) {
   try {
    p(readPpt(“src/1.dps”));
   } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }

}
//读取ppt
public static String readPpt(String path) throws Exception {

   StringBuffer content = new StringBuffer(“”);
   try {
    SlideShow ss = new SlideShow(new HSLFSlideShow(path));//
path为文件的全路径名称,建立SlideShow
    Slide[] slides = ss.getSlides();// 得到每一张幻灯片
    for (int i = 0; i < slides.length; i++) {
     TextRun[] t = slides[i].getTextRuns();//
为了拿走幻灯片的文字内容,建立TextRun
     for (int j = 0; j < t.length; j++) {
      content.append(t[j].getText());//
那里会将文字内容加到content中去
     }
     content.append(slides[i].getTitle());
    }
   } catch (Exception ex) {
    System.out.println(ex.toString());
   }
   return content.toString().trim();

}
// 读取xls
public static String readXls(String path) throws Exception {
   StringBuffer content = new StringBuffer(“”);// 文档内容
   HSSFWorkbook workbook = new HSSFWorkbook(new
FileInputStream(path));
   int sheetCount = workbook.getNumberOfSheets();// excel几张表
   for (int i = 0; i < sheetCount; i++) {// 遍历excel表
    HSSFSheet sheet = workbook.getSheetAt(i);// 对excel的第②个表引用
    int rowCount = sheet.getLastRowNum();// 取得最终一行的下标
    for (int j = 0; j < rowCount; j++) {// 循环每一行
     HSSFRow row = sheet.getRow(j);// 引用行
     if (row == null) {
      continue;
     } else {
      short cellNum = row.getLastCellNum();
      for (short m = 0; m < cellNum; m++) {
       HSSFCell cell = row.getCell(m);// 引用行中的三个单元格
       if (cell != null) {
        int cellType = cell.getCellType();
        // CELL_TYPE_NUMERIC 0 数字
        // CELL_TYPE_STRING 1 字符串
        // CELL_TYPE_FORMULA 2 公式
        // CELL_TYPE_BLANK 3 空格
        // CELL_TYPE_BOOLEAN 4 布尔值
        // CELL_TYPE_ERROR 5 错误
        switch (cellType) {
        // 单元格类型为数字
        case HSSFCell.CELL_TYPE_NUMERIC:
         // 取数字单元格的值
         double d = cell.getNumericCellValue();
         content.append(String.valueOf(d) + ”   “);
         break;
        // 单元格类型为字符串
        case HSSFCell.CELL_TYPE_STRING:
         String str = cell.getStringCellValue().trim();
         if (!str.equals(“”)) {
          content.append(str + ”   “);
         }
         break;
        // 单元格类型为公式
        case HSSFCell.CELL_TYPE_FORMULA:
         // 不读取公式
         // String formula = cell.getCellFormula();
         // content = content + formula+” “;
         break;
        // 单元格类型为空白
        case HSSFCell.CELL_TYPE_BLANK:
         break;
        // 单元格类型为布尔值
        case HSSFCell.CELL_TYPE_BOOLEAN:
         // boolean bool = cell.getBooleanCellValue();
         // content = content + bool+” “;
         break;
        // 单元格类型为不当
        case HSSFCell.CELL_TYPE_ERROR:
         // byte errorCode = cell.getErrorCellValue();
         // content = content + errorCode+” “;
         break;
        default:
         break;
        }
       } else {
        // content = content + “…” +”
“;//没有数量的单元格使用…填充
       }
      }
     }
     content.append(“\r”);
    }
   }
   return content.toString().trim();
}

// 读取pdf
public static String readPdf(String path) throws Exception {
   StringBuffer content = new StringBuffer(“”);// 文档内容
   FileInputStream fis = new FileInputStream(path);
   PDFParser p = new PDFParser(fis);
   p.parse();
   PDFTextStripper ts = new PDFTextStripper();
   content.append(ts.getText(p.getPDDocument()));
   fis.close();
   return content.toString().trim();
}

// 读取word,只可以读取文本内容 图片不行
public static String readWord(String path) throws Exception {

   StringBuffer content = new StringBuffer(“”);// 文档内容
   HWPFDocument doc = new HWPFDocument(new FileInputStream(path));
   Range range = doc.getRange();
   int paragraphCount = range.numParagraphs();// 段落
   for (int i = 0; i < paragraphCount; i++) {// 遍历段落读取数据
    Paragraph pp = range.getParagraph(i);
    content.append(pp.text());
   }
   return content.toString().trim();
}

// 读取text
public static String readTxt(String path) {
   StringBuffer content = new StringBuffer(“”);// 文档内容
   try {
    FileReader reader = new FileReader(path);
    BufferedReader br = new BufferedReader(reader);
    String s1 = null;

    while ((s1 = br.readLine()) != null) {
     content.append(s1 + “\r”);
    }
    br.close();
    reader.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
   return content.toString().trim();
}

}

<dependency>
  <groupId>com.belerweb</groupId>
  <artifactId>pinyin4j</artifactId>

        } catch (FileNotFoundException e) {
            System.out.println(“未找到指定路线的文本!”);
            e.printStackTrace();
        }
    }
}

4:excel转PDF字段内容无故中间某些换行:

 

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)throws Exception {
        read(“d:\\demo.xls”);
    }

2:excel转pdf出现折行。

public static String resetXmlProperty(String xmlStr,String
companySpell,String projectName){

复制代码 代码如下:

 public static boolean getLicense()
{
        boolean result = false;
        try {
            InputStream is =
Thread.currentThread().getContextClassLoader()
                  
 .getResourceAsStream(“license.xml”); // 
license.xml应放在..\WebRoot\WEB-INF\classes路径下
            License aposeLic = new
License();
           
aposeLic.setLicense(is);
            result = true;
        } catch (Exception e)
{               
            e.printStackTrace();
        }
        return result;
    }
    
    
    public static void
excelTransferPdf(String excelPath,String pdfPath) {
        if (!getLicense()) {
            System.out.println(“license
faile”);
            return;
        }
        
        try {     
            Workbook wb = new
Workbook(excelPath);
            FileOutputStream fileOS = new
FileOutputStream(new File(pdfPath));
            wb.save(fileOS,
com.aspose.cells.SaveFormat.PDF);
            fileOS.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

package edu.sjtu.erplab.poi;

  那是本人遇上的最坑的三个地点,这么些时候你只必要在excel单元格里面安装自动换行即可,无需代码强行自动换行(强行换行有大概只现出多行数据只显示一行)。同时你需求如下代码:

/**设置xml文件属性的值  (xml文件是壹个字符串的形式)*/

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

  当你未安装单元格大小而又从不设置单元格自动换行,比如您的A18单元格里面的字段超越了单元格的长度你还尚未设置单元格大小而又没有设置单元格自动换行,就将抓取的字段填充在B18单元格里面,那么打印出来的pdf文件A18单元格超出单元万分的故事情节是置之脑后显示的,此时你依然将抓取字段填充在C18单元格内要么将更改A18单元格格式

<dependency>
  <groupId>net.sourceforge.pinyin4j</groupId>
  <artifactId>pinyin4j</artifactId>

    public static ArrayList read(String fileName){
        ArrayList list = new ArrayList();
        String sql = “”;
        try{
            File f = new File(fileName);
            FileInputStream fis = new FileInputStream(f);
            HSSFWorkbook wbs = new HSSFWorkbook(fis);
            HSSFSheet childSheet = wbs.getSheetAt(0);
            System.out.println(“行数:” + childSheet.getLastRowNum());
            for(int i = 4;i<childSheet.getLastRowNum();i++){
                HSSFRow row = childSheet.getRow(i);
                System.out.println(“列数:” +
row.getPhysicalNumberOfCells());
                if(null != row){
                    for(int
k=1;k<row.getPhysicalNumberOfCells();k++){
                        HSSFCell cell;
                        cell = row.getCell((short)k);
                       // System.out.print(getStringCellValue(cell) +
“\t”);
                        list.add(getStringCellValue(cell) + “\t”);
                    }
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return list;
    }
    /**
     * 获取单元格数据内容为字符串类型的数量
     *
     * @param cell Excel单元格
     * @return String 单元格数据内容
     */
    private static String getStringCellValue(HSSFCell cell) {
        String strCell = “”;
        switch (cell.getCellType()) {
        case HSSFCell.CELL_TYPE_STRING:
            strCell = cell.getStringCellValue();
            break;
        case HSSFCell.CELL_TYPE_NUMERIC:
            strCell = String.valueOf(cell.getNumericCellValue());
            break;
        case HSSFCell.CELL_TYPE_BOOLEAN:
            strCell = String.valueOf(cell.getBooleanCellValue());
            break;
        case HSSFCell.CELL_TYPE_BLANK:
            strCell = “”;
            break;
        default:
            strCell = “”;
            break;
        }
        if (strCell.equals(“”) || strCell == null) {
            return “”;
        }
        if (cell == null) {
            return “”;
        }
        return strCell;
    }
}

    /**
     * 根据字符串长度获取行高
     *
     * @param str
     * @return
     */
    public static Float getRowHeight(String str) {

</dependency>

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
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.poifs.filesystem.POIFSFileSystem;

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图