public static void readExcel2007()
{
InputStream in = null;
try
{
in = new FileInputStream("d:\\b.xlsx");
// 创建work
Workbook work = new XSSFWorkbook(in);//主要在这里报错
// 得到第一个sheet
Sheet sheet = work.getSheetAt(0);
// 获取总行数
int rowNum = sheet.getLastRowNum();
Row row = null;
// 循环每一行
for (int i = 1; i <= rowNum; i++)
{
row = sheet.getRow(i);
if (null == row)
{
continue;
}
Cell cell = row.getCell(0);
if (cell != null)
{
System.out.println(cell.getStringCellValue());
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
readExcel2003();
}异常信息:
java.lang.IllegalStateException: The hyperlink for cell A2 references relation rId1, but that didn't exist!
at org.apache.poi.xssf.usermodel.XSSFHyperlink.<init>(XSSFHyperlink.java:70)
at org.apache.poi.xssf.usermodel.XSSFSheet.initHyperlinks(XSSFSheet.java:250)
at org.apache.poi.xssf.usermodel.XSSFSheet.read(XSSFSheet.java:203)
at org.apache.poi.xssf.usermodel.XSSFSheet.onDocumentRead(XSSFSheet.java:175)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.onDocumentRead(XSSFWorkbook.java:227)
at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:159)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:181)
at ImportExcel.ImportExcelDemo.readExcel2007(ImportExcelDemo.java:73)
at ImportExcel.ImportExcelDemo.main(ImportExcelDemo.java:103)Excel文件: 其中[email protected]是超链接(也不知道为什么,打上去自动就是超链接)Account Name
[email protected] 张杰
jjke gf
{
InputStream in = null;
try
{
in = new FileInputStream("d:\\b.xlsx");
// 创建work
Workbook work = new XSSFWorkbook(in);//主要在这里报错
// 得到第一个sheet
Sheet sheet = work.getSheetAt(0);
// 获取总行数
int rowNum = sheet.getLastRowNum();
Row row = null;
// 循环每一行
for (int i = 1; i <= rowNum; i++)
{
row = sheet.getRow(i);
if (null == row)
{
continue;
}
Cell cell = row.getCell(0);
if (cell != null)
{
System.out.println(cell.getStringCellValue());
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
readExcel2003();
}异常信息:
java.lang.IllegalStateException: The hyperlink for cell A2 references relation rId1, but that didn't exist!
at org.apache.poi.xssf.usermodel.XSSFHyperlink.<init>(XSSFHyperlink.java:70)
at org.apache.poi.xssf.usermodel.XSSFSheet.initHyperlinks(XSSFSheet.java:250)
at org.apache.poi.xssf.usermodel.XSSFSheet.read(XSSFSheet.java:203)
at org.apache.poi.xssf.usermodel.XSSFSheet.onDocumentRead(XSSFSheet.java:175)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.onDocumentRead(XSSFWorkbook.java:227)
at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:159)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:181)
at ImportExcel.ImportExcelDemo.readExcel2007(ImportExcelDemo.java:73)
at ImportExcel.ImportExcelDemo.main(ImportExcelDemo.java:103)Excel文件: 其中[email protected]是超链接(也不知道为什么,打上去自动就是超链接)Account Name
[email protected] 张杰
jjke gf
你代码给出的是readExcel2007 但是 main里面运行的是readExcel2003();
这是不是很奇怪呢?
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;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;import java.io.InputStream;
import java.io.FileInputStream;/**
* Created by IntelliJ IDEA.
* User: admin
* Date: 2011-9-21
* Time: 17:19:14
* To change this template use File | Settings | File Templates.
*/
public class ExcelPOITest {
public static void readExcel2007()
{
String filepath = "d:\\b.xlsx";
FileInputStream fis = null;
try {
fis = new FileInputStream(filepath);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
XSSFWorkbook wb=null;
try { wb = new XSSFWorkbook(fis); } catch (IOException e3) {
e3.printStackTrace();
}
try {
fis.close();
} catch (IOException e1) {
e1.printStackTrace();
} XSSFSheet sheet = wb.getSheetAt(0);
XSSFRow row = null;
XSSFCell cell = null; String a1 = "";
String a2 ="";
int rowNum; rowNum = sheet.getLastRowNum();
for (int i = 1; i <= rowNum; i++) {
row = sheet.getRow(i);
cell = row.getCell(0);
a1 = cell.getStringCellValue();
cell = row.getCell(1);
a2 = cell.getStringCellValue();
System.out.println(a1+"\t"+a2+"\t"); }
}public static void main(String[] args)
{
readExcel2007();
}
}
你能不能参考我给你写的代码,以这个为基础改,这个对@ $$% 处理没有任何问题我已经说了N遍,Workbook 这一套不行, 要用XSSF对应代码是XSSFWorkbook work=new XSSFWorkbook(in)你直接Workbook work = new XSSFWorkbook(in);肯定报出来强制类型转换错误
public static void readExcelOf2007()
{
String filepath = "d:\\b.xlsx";
FileInputStream fis = null;
try
{
fis = new FileInputStream(filepath);
}
catch (FileNotFoundException e1)
{
e1.printStackTrace();
}
XSSFWorkbook wb = null;
try
{
wb = new XSSFWorkbook(fis);
}
catch (IOException e3)
{
e3.printStackTrace();
}
try
{
fis.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
XSSFSheet sheet = wb.getSheetAt(0);
XSSFRow row = null;
XSSFCell cell = null;
String a1 = "";
String a2 = "";
int rowNum;
rowNum = sheet.getLastRowNum();
for (int i = 1; i <= rowNum; i++)
{
row = sheet.getRow(i);
if (row != null)
{
cell = row.getCell(0);
a1 = cell == null ? "" : cell.getStringCellValue();
cell = row.getCell(1);
a2 = cell == null ? "" : cell.getStringCellValue();
System.out.println(a1 + "\t" + a2 + "\t");
}
}
}
public static void main(String[] args)
{
readExcelOf2007();
}
异常信息:
Exception in thread "main" java.lang.IllegalStateException: The hyperlink for cell A2 references relation rId1, but that didn't exist!
at org.apache.poi.xssf.usermodel.XSSFHyperlink.<init>(XSSFHyperlink.java:70)
at org.apache.poi.xssf.usermodel.XSSFSheet.initHyperlinks(XSSFSheet.java:250)
at org.apache.poi.xssf.usermodel.XSSFSheet.read(XSSFSheet.java:203)
at org.apache.poi.xssf.usermodel.XSSFSheet.onDocumentRead(XSSFSheet.java:175)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.onDocumentRead(XSSFWorkbook.java:227)
at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:159)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:181)
at ImportExcel.ImportExcelDemo.readExcelOf2007(ImportExcelDemo.java:125)
at ImportExcel.ImportExcelDemo.main(ImportExcelDemo.java:170)
:
Account Name User Type Region Representative Office Email Telephone IsResponsible Re
!#$%$#^%$#^%$&%^*&^*(*&(&*) 姓名中包含特殊字符 Regional Software Service Director Northern Africa [email protected] 123 "备注含有中文、字母、特殊字符、空格、 换行
!@#$%^&*()_+{}|:""<>?!@#¥%……&*()——+{}|:“《》?<script>alert();中意!</script><table></table>
88<script type =""text/javascipt"" src =""/ci-master/d"">et=utf-8"" /><meta ht备注备注备注备注tp-equiv=""Content-Type"" content=""text/html; charset=utf-8"" /><meta ht备注备注备注备注tp-equiv=""Content-Type"" content=""text/html; charset=utf-8"" /><meta ht备注备注备注备注tp-equiv=""Content-Type"" content=""text/html; charset=utf-8"" />"
jjke gfgfgf Regional Software Service Director Northern Africa [email protected] ggfgf gf
jjkyetyuu gfgfgf Regional Software Service Director Northern Africa [email protected] ggfgf gf
dffdedf 姓名中包含特殊字符 Regional Software Service Director Northern Africa [email protected] 123 "备注含有中文、字母、特殊字符、空格、 换行
!@#$%^&*()_+{}|:""<>?!@#¥%……&*()——+{}|:“《》?<script>alert();中意!</script><table></table>
88<script type =""text/javascipt"" src =""/ci-master/d"">et=utf-8"" /><meta ht备注备注备注备注tp-equiv=""Content-Type"" content=""text/html; charset=utf-8"" /><meta ht备注备注备注备注tp-equiv=""Content-Type"" content=""text/html; charset=utf-8"" /><meta ht备注备注备注备注tp-equiv=""Content-Type"" content=""text/html; charset=utf-8"" />"
dffddfsss 姓名中包含特殊字符 Regional Software Service Director Northern Africa [email protected] 123 "备注含有中文、字母、特殊字符、空格、 换行
!@#$%^&*()_+{}|:""<>?!@#¥%……&*()——+{}|:“《》?<script>alert();中意!</script><table></table>
88<script type =""text/javascipt"" src =""/ci-master/d"">et=utf-8"" /><meta ht备注备注备注备注tp-equiv=""Content-Type"" content=""text/html; charset=utf-8"" /><meta ht备注备注备注备注tp-equiv=""Content-Type"" content=""text/html; charset=utf-8"" /><meta ht备注备注备注备注tp-equiv=""Content-Type"" content=""text/html; charset=utf-8"" />"
002 1 Regional Software Service Director China [email protected]
name password
!@$#@$#@%#$%^$#@@#%$@#%#$^$%&%^*&^(* !$@##$&%*^&(&*)(*&)(*_)*(_)(_%%#$@!$#@!$@#!
urlCell.setCellFormula("HYPERLINK(\"" + item.getUrl() + "\",\"" + name + "\")");
公式中的单个字符串长度最大值为255 还有其他方法设置超链接的吗?
http://poi.apache.org/changes.htmllz试试POI BETA6不是$的问题,只是读取Workbook,与流有关