为啥java读不了.xlsx的excel文件只能读.xls的,难道是因为我的jxl.jar包太久了?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为啥java读不了.xlsx的excel文件只能读.xls的,难道是因为我的jxl.jar包太久了?相关的知识,希望对你有一定的参考价值。

网上说最新版的下载下来也没有用,有没有大神有解决方法啊?如果有可以读.xlsx的jxl.jar请发到我的邮箱774524998@qq.com,万分感谢

参考技术A 建议您还是说服用户把文档格式改成2003吧。。。jxl暂时没JAR包支持2007或2010.。。
当然换POI 可行,但整个项目里凡是以前用到jxl的地方都要换。。。风险是不是太大了。。
参考技术B 兄弟 jxl不支持office2007
还是用Apache POI 插件吧本回答被提问者采纳
参考技术C 百度知道不让发网址。你在百度里搜索:大气象
在我的博客园博客里有详细代码。
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.htmlControls;

using System.IO;
using System.Reflection;
using Microsoft.Office.Interop.Excel;

public partial class _Default : System.Web.UI.Page

protected void Page_Load(object sender, EventArgs e)

if (!IsPostBack)
Bind();

private void Bind()

//模板文件
string TempletFileName = Server.MapPath("template/") + "template.xlsx";
//导出文件
string ReportFileName = Server.MapPath("xls/") + "out.xlsx";

string strTempletFile = Path.GetFileName(TempletFileName);
//将模板文件复制到输出文件
FileInfo mode = new FileInfo(TempletFileName);
mode.CopyTo(ReportFileName, true);

//打开excel
object missing = Missing.Value;
Application app = null;
Workbook wb = null;
Worksheet ws = null;
Range r = null;
//
app = new Microsoft.Office.Interop.Excel.Application();
wb = app.Workbooks.Open(ReportFileName, false, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
app.Visible = true;

//得到WorkSheet对象
ws = (Worksheet)wb.Worksheets.get_Item(1);

//添加或修改WorkSheet里的数据
ws.Cells[1, 1] = "100";
ws.Cells[2, 1] = "100";
ws.Cells[2, 2] = "100";
//代码里写个公式
r = (Range)ws.Cells[2, 3];
r.Formula = "=A2*B2";

//输出Excel文件并退出
wb.Save();
wb.Close(null, null, null);
app.Workbooks.Close();
app.Application.Quit();
app.Quit();

System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);

ws = null;
wb = null;
app = null;


java 使用Apache POI读/写Excel文件(.xls或.xlsx)

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

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.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadWriteExcelFile {

	public static void readXLSFile() throws IOException
	{
		InputStream ExcelFileToRead = new FileInputStream("C:/Test.xls");
		HSSFWorkbook wb = new HSSFWorkbook(ExcelFileToRead);

		HSSFSheet sheet=wb.getSheetAt(0);
		HSSFRow row; 
		HSSFCell cell;

		Iterator rows = sheet.rowIterator();

		while (rows.hasNext())
		{
			row=(HSSFRow) rows.next();
			Iterator cells = row.cellIterator();
			
			while (cells.hasNext())
			{
				cell=(HSSFCell) cells.next();
		
				if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING)
				{
					System.out.print(cell.getStringCellValue()+" ");
				}
				else if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)
				{
					System.out.print(cell.getNumericCellValue()+" ");
				}
				else
				{
					//U Can Handel Boolean, Formula, Errors
				}
			}
			System.out.println();
		}
	
	}
	
	public static void writeXLSFile() throws IOException {
		
		String excelFileName = "C:/Test.xls";//name of excel file

		String sheetName = "Sheet1";//name of sheet

		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet(sheetName) ;

		//iterating r number of rows
		for (int r=0;r < 5; r++ )
		{
			HSSFRow row = sheet.createRow(r);
	
			//iterating c number of columns
			for (int c=0;c < 5; c++ )
			{
				HSSFCell cell = row.createCell(c);
				
				cell.setCellValue("Cell "+r+" "+c);
			}
		}
		
		FileOutputStream fileOut = new FileOutputStream(excelFileName);
		
		//write this workbook to an Outputstream.
		wb.write(fileOut);
		fileOut.flush();
		fileOut.close();
	}
	
	public static void readXLSXFile() throws IOException
	{
		InputStream ExcelFileToRead = new FileInputStream("C:/Test.xlsx");
		XSSFWorkbook  wb = new XSSFWorkbook(ExcelFileToRead);
		
		XSSFWorkbook test = new XSSFWorkbook(); 
		
		XSSFSheet sheet = wb.getSheetAt(0);
		XSSFRow row; 
		XSSFCell cell;

		Iterator rows = sheet.rowIterator();

		while (rows.hasNext())
		{
			row=(XSSFRow) rows.next();
			Iterator cells = row.cellIterator();
			while (cells.hasNext())
			{
				cell=(XSSFCell) cells.next();
		
				if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING)
				{
					System.out.print(cell.getStringCellValue()+" ");
				}
				else if(cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC)
				{
					System.out.print(cell.getNumericCellValue()+" ");
				}
				else
				{
					//U Can Handel Boolean, Formula, Errors
				}
			}
			System.out.println();
		}
	
	}
	
	public static void writeXLSXFile() throws IOException {
		
		String excelFileName = "C:/Test.xlsx";//name of excel file

		String sheetName = "Sheet1";//name of sheet

		XSSFWorkbook wb = new XSSFWorkbook();
		XSSFSheet sheet = wb.createSheet(sheetName) ;

		//iterating r number of rows
		for (int r=0;r < 5; r++ )
		{
			XSSFRow row = sheet.createRow(r);

			//iterating c number of columns
			for (int c=0;c < 5; c++ )
			{
				XSSFCell cell = row.createCell(c);
	
				cell.setCellValue("Cell "+r+" "+c);
			}
		}

		FileOutputStream fileOut = new FileOutputStream(excelFileName);

		//write this workbook to an Outputstream.
		wb.write(fileOut);
		fileOut.flush();
		fileOut.close();
	}

	public static void main(String[] args) throws IOException {
		
		writeXLSFile();
		readXLSFile();
		
		writeXLSXFile();
		readXLSXFile();

	}

}

以上是关于为啥java读不了.xlsx的excel文件只能读.xls的,难道是因为我的jxl.jar包太久了?的主要内容,如果未能解决你的问题,请参考以下文章

java 使用Apache POI读/写Excel文件(.xls或.xlsx)

java 使用Apache POI读/写Excel文件(.xls或.xlsx)

JasperReports xlsx 导出错误 - 内容不可读 - 为啥?

java 读excel xlsx

用java读excel的数据。

读/写xlsx文件