poi3.16读取xlsx excel2007+的官方例子

http://poi.apache.org/spreadsheet/converting.html

New, generic SS Usermodel Code
// import org.apache.poi.ss.usermodel.*;

Workbook[] wbs = new Workbook[] { new HSSFWorkbook(), new XSSFWorkbook() };
for(int i=0; i<wbs.length; i++) {
   Workbook wb = wbs[i];
   CreationHelper createHelper = wb.getCreationHelper();

   // create a new sheet
   Sheet s = wb.createSheet();
   // declare a row object reference
   Row r = null;
   // declare a cell object reference
   Cell c = null;
   // create 2 cell styles
   CellStyle cs = wb.createCellStyle();
   CellStyle cs2 = wb.createCellStyle();
   DataFormat df = wb.createDataFormat();

   // create 2 fonts objects
   Font f = wb.createFont();
   Font f2 = wb.createFont();

   // Set font 1 to 12 point type, blue and bold
   f.setFontHeightInPoints((short) 12);
   f.setColor( IndexedColors.RED.getIndex() );
   f.setBoldweight(Font.BOLDWEIGHT_BOLD);

   // Set font 2 to 10 point type, red and bold
   f2.setFontHeightInPoints((short) 10);
   f2.setColor( IndexedColors.RED.getIndex() );
   f2.setBoldweight(Font.BOLDWEIGHT_BOLD);

   // Set cell style and formatting
   cs.setFont(f);
   cs.setDataFormat(df.getFormat("#,##0.0"));

   // Set the other cell style and formatting
   cs2.setBorderBottom(cs2.BORDER_THIN);
   cs2.setDataFormat(df.getFormat("text"));
   cs2.setFont(f2);


   // Define a few rows
   for(int rownum = 0; rownum < 30; rownum++) {
	   Row r = s.createRow(rownum);
	   for(int cellnum = 0; cellnum < 10; cellnum += 2) {
		   Cell c = r.createCell(cellnum);
		   Cell c2 = r.createCell(cellnum+1);
   
		   c.setCellValue((double)rownum + (cellnum/10));
		   c2.setCellValue(
		         createHelper.createRichTextString("Hello! " + cellnum)
		   );
	   }
   }
   
   // Save
   String filename = "workbook.xls";
   if(wb instanceof XSSFWorkbook) {
     filename = filename + "x";
   }
 
   FileOutputStream out = new FileOutputStream(filename);
   wb.write(out);
   out.close();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android应用程序中使用Apache POI读取高版本的Excel(.xlsx)文件,您需要添加相应的依赖项并使用XSSFWorkbook类来处理xlsx文件。以下是一个示例代码,演示了如何读取xlsx文件: ```java import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.IOException; import java.io.InputStream; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 从assets目录下读取xlsx文件 try { InputStream inputStream = getAssets().open("example.xlsx"); // 使用XSSFWorkbook打开xlsx文件 XSSFWorkbook workbook = new XSSFWorkbook(inputStream); // 获取第一个Sheet Sheet sheet = workbook.getSheetAt(0); // 遍历行 for (Row row : sheet) { // 遍历单元格 for (Cell cell : row) { // 获取单元格值并打印 String cellValue = cell.toString(); System.out.println("Cell value: " + cellValue); } } // 关闭输入流和工作簿 inputStream.close(); workbook.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 在这个示例中,我们假设您将xlsx文件放置在assets目录下,并命名为"example.xlsx"。您可以根据实际情况修改文件名和路径。 请确保在您的项目中添加了Apache POI库的依赖项。您可以在build.gradle文件中的dependencies部分添加以下行来添加依赖项: ```groovy implementation 'org.apache.poi:poi:4.1.2' implementation 'org.apache.poi:poi-ooxml:4.1.2' ``` 这样,您就可以在Android应用程序中使用Apache POI读取高版本的Excel(.xlsx)文件。请根据您的实际需求进行适当的异常处理和数据处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值