问题:
using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
{
//XSSFWorkbook 适用XLSX格式,HSSFWorkbook 适用XLS格式
if (fileExt == ".xlsx")
{
workbook = new XSSFWorkbook(fs);//此处会报错提示The hyperlink for cell I2 references relation rId1, but that didn't exist!(单元格引用的超链接关系不存在!)
}
else if (fileExt == ".xls")
{
workbook = new HSSFWorkbook(fs);
}
else
{
workbook = null;
Console.WriteLine(string.Format("未选择正确的xlsx文件-{0}", DateTime.Now));
}
if (workbook == null)
{
return null;
}
//得到excel的sheet数
for (int k = 0; k < workbook.NumberOfSheets; k++)
{
if (workbook.IsSheetHidden(k))
{
continue;
}
DataTable dt = new DataTable();
ISheet sheet = workbook.GetSheetAt(k);
//表头
IRow header = sheet.GetRow(sheet.FirstRowNum);
List<int> columns = new List<int>();
for (int i = 0; i < header.LastCellNum; i++)
{
object obj = GetValueType(header.GetCell(i), workbook);
if (obj == null || obj.ToString() == string.Empty)
{
dt.Columns.Add(new DataColumn("Columns" + (i + 1).ToString()));
Console.WriteLine(string.Format("Excel文件{0}中{1}列表头不存在", sheet.SheetName, (i + 1).ToString()));
}
else
{
if (dt.Columns.Contains(obj.ToString()))
{
obj = obj + i.ToString();
}
dt.Columns.Add(new DataColumn(obj.ToString()));
}
columns.Add(i);
}
//数据
for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++)
{
DataRow dr = dt.NewRow();
bool hasValue = false;
if (sheet.GetRow(i) != null)
{
foreach (int j in columns)
{
dr[j] = GetValueType(sheet.GetRow(i).GetCell(j), workbook);
if (dr[j] != null && dr[j].ToString() != string.Empty)
{
hasValue = true;
}
}
if (hasValue)
{
dt.Rows.Add(dr);
}
}
else
{
Console.WriteLine(string.Format("Excel文件{0}中{1}行数据为空", sheet.SheetName, i.ToString()));
}
}
dict.Add(sheet.SheetName, dt);
}
}
[(https://img2018.cnblogs.com/q/1244030/201910/1244030-20191023131850349-637510532.png)]
怎么去处理这个带有超链接格式的文本呢?求教求教
回答1:
npoi 貌似没有办法解决,java版本 3.9以上fix了这个问题。
可以考虑从source code打一个包 fix。
https://github.com/tonyqus/npoi/blob/master/ooxml/XSSF/UserModel/XSSFHyperlink.cs#L77
回答2:
你可以尝试使用Spire.XLS来读取