C# 使用NPOI读取Excel单元格内容

Simeon8888 2016-04-15 04:51:23


用NPOI 如何读取 图中单元格 不带删除线的部分 如何区分
...全文
1378 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
zlleige 2017-08-01
  • 打赏
  • 举报
回复
请问NPOi能够读取excel的单元格名称么,进而用excel单元格名称导入数据
xzg1109 2016-07-25
  • 打赏
  • 举报
回复
引用 楼主 Simeon8888 的回复:
用NPOI 如何读取 图中单元格 不带删除线的部分 如何区分
引用 1 楼 RubenLuLu 的回复:
不使用NPOI我倒是会。然而NPOI我没接触过
大神,不使用NPOI,其它什么方法可以实现?
Poopaye 2016-04-28
  • 打赏
  • 举报
回复
我研究了下,因为AAAA是没有字体的,所以获取的时候。。。应该算是个bug吧 只能这样分析
IRow row = sheet.GetRow(i);
ICell cell = row.GetCell(0);
XSSFRichTextString richText = (XSSFRichTextString)cell.RichStringCellValue;
var ctrst = richText.GetCTRst();

Console.WriteLine("row:{0}, col:{1}, val:{2}, rlen:{3}", i, 0, richText.String, ctrst.r.Count);

if (ctrst.r.Count > 0)
{
	foreach (var item in ctrst.r)
	{
		bool isStrike;
		if (item.rPr != null && item.rPr.strike != null)
			isStrike = item.rPr.strike.val;
		else
			isStrike = cell.CellStyle.GetFont(book).IsStrikeout;
		Console.WriteLine("\t string:" + item.t + ", isStrike:" + isStrike); //item.t是对应的字符串
	}
}
else
	Console.WriteLine("\t isStrike: " + cell.CellStyle.GetFont(book).IsStrikeout);
Simeon8888 2016-04-28
  • 打赏
  • 举报
回复
引用 13 楼 mjp1234airen4385 的回复:
难道是因为既有删除线,又没有删除线,所以才出问题的。
*.xls 这个版本的没有问题 *.xlsx 这个有问题 我是要取 其中没有删除线的部分
mjp1234airen4385 2016-04-28
  • 打赏
  • 举报
回复
难道是因为既有删除线,又没有删除线,所以才出问题的。
Simeon8888 2016-04-28
  • 打赏
  • 举报
回复
引用 11 楼 shingoscar 的回复:
HSSF对应xls XSSF对应xlsx 用2.1.3.1 2.2还是alpha阶段
还是不能用 2007 这个地方 就报错了

IRichTextString richTextString = cell.RichStringCellValue;
                        for (int j = 0; j < richTextString.Length; ++j)
                        {
                            short a = richTextString.GetFontAtIndex(j);
                            if (a > 0)
                            {
                                IFont font = workBook.GetFontAt(a);

                                if (font.IsStrikeout)
                                {
                                    //位于i上的字符是带删除线的
                                    s3 += richTextString.ToString().Substring(j, 1);
                                }
                            }
                        }

图中 第三条 就直接报错了 xlsx的版本
Simeon8888 2016-04-28
  • 打赏
  • 举报
回复
引用 15 楼 shingoscar 的回复:
我研究了下,因为AAAA是没有字体的,所以获取的时候。。。应该算是个bug吧 只能这样分析
IRow row = sheet.GetRow(i);
ICell cell = row.GetCell(0);
XSSFRichTextString richText = (XSSFRichTextString)cell.RichStringCellValue;
var ctrst = richText.GetCTRst();

Console.WriteLine("row:{0}, col:{1}, val:{2}, rlen:{3}", i, 0, richText.String, ctrst.r.Count);

if (ctrst.r.Count > 0)
{
	foreach (var item in ctrst.r)
	{
		bool isStrike;
		if (item.rPr != null && item.rPr.strike != null)
			isStrike = item.rPr.strike.val;
		else
			isStrike = cell.CellStyle.GetFont(book).IsStrikeout;
		Console.WriteLine("\t string:" + item.t + ", isStrike:" + isStrike); //item.t是对应的字符串
	}
}
else
	Console.WriteLine("\t isStrike: " + cell.CellStyle.GetFont(book).IsStrikeout);
好吧 谢谢
Poopaye 2016-04-26
  • 打赏
  • 举报
回复
HSSF对应xls XSSF对应xlsx 用2.1.3.1 2.2还是alpha阶段
Simeon8888 2016-04-26
  • 打赏
  • 举报
回复
引用 7 楼 shingoscar 的回复:
[quote=引用 4 楼 Simeon8888 的回复:] [quote=引用 2 楼 shingoscar 的回复:]
IRichTextString richTextString = cell.RichStringCellValue;
for(int i = 0; i < richTextString.Length; ++i)
{
    IFont font = workbook.GetFontAt(richTextString.GetFontAtIndex(i));
    if(font.IsStrikeout)
         //位于i上的字符是带删除线的
}
这个是针对Office 2003 的版本的吧 Office 2007好像不行[/quote] 那就贴出你的代码来 上面写的都是和版本无关的代码,你所谓的2007好像不行纯粹是你自己的问题[/quote]
Simeon8888 2016-04-26
  • 打赏
  • 举报
回复
用的NPOI 2.2.0.0 这个版本的[/quote]
引用 7 楼 shingoscar 的回复:
[quote=引用 4 楼 Simeon8888 的回复:] [quote=引用 2 楼 shingoscar 的回复:]
IRichTextString richTextString = cell.RichStringCellValue;
for(int i = 0; i < richTextString.Length; ++i)
{
    IFont font = workbook.GetFontAt(richTextString.GetFontAtIndex(i));
    if(font.IsStrikeout)
         //位于i上的字符是带删除线的
}
这个是针对Office 2003 的版本的吧 Office 2007好像不行[/quote] 那就贴出你的代码来 上面写的都是和版本无关的代码,你所谓的2007好像不行纯粹是你自己的问题[/quote]


  IRichTextString richTextString = cell.RichStringCellValue;
                        for (int j = 0; j < richTextString.Length; ++j)
                        {
                            IFont font = workBook.GetFontAt(
                                richTextString.GetFontAtIndex(j));

                            if (font.IsStrikeout)
                            {
                                //位于i上的字符是带删除线的
                                //ss += xssfRichTextString.ToString().Substring(j, 1);
                            }
                        }

Simeon8888 2016-04-26
  • 打赏
  • 举报
回复
引用 7 楼 shingoscar 的回复:
[quote=引用 4 楼 Simeon8888 的回复:] [quote=引用 2 楼 shingoscar 的回复:]
IRichTextString richTextString = cell.RichStringCellValue;
for(int i = 0; i < richTextString.Length; ++i)
{
    IFont font = workbook.GetFontAt(richTextString.GetFontAtIndex(i));
    if(font.IsStrikeout)
         //位于i上的字符是带删除线的
}
这个是针对Office 2003 的版本的吧 Office 2007好像不行[/quote] 那就贴出你的代码来 上面写的都是和版本无关的代码,你所谓的2007好像不行纯粹是你自己的问题[/quote]


                        if (hssfRichTextString != null)
                        {
                            string ss = "";
                            hssfRichTextString = (HSSFRichTextString)cell.RichStringCellValue;
                            for (int j = 0; j < hssfRichTextString.Length; ++j)
                            {
                                IFont font = workBook.GetFontAt(hssfRichTextString.GetFontAtIndex(j));

                                if (font.IsStrikeout)
                                {
                                    //位于i上的字符是带删除线的
                                    ss += hssfRichTextString.ToString().Substring(j, 1);
                                }
                            }
                            MessageBox.Show(ss, i + "");
                        }

                        if (xssfRichTextString != null)
                        {
                            string ss = "";
                            xssfRichTextString =(XSSFRichTextString) cell.RichStringCellValue;
                            for (int j = 0; j < xssfRichTextString.Length; ++j)
                            {
                                IFont font = workBook.GetFontAt(xssfRichTextString.GetFontAtIndex(j));

                                if (font.IsStrikeout)
                                {
                                    //位于i上的字符是带删除线的
                                    ss += xssfRichTextString.ToString().Substring(j, 1);
                                }
                            }
                            MessageBox.Show(ss, i + "");
                        }
用的NPOI 2.2.0.0 这个版本的
Poopaye 2016-04-25
  • 打赏
  • 举报
回复
引用 4 楼 Simeon8888 的回复:
[quote=引用 2 楼 shingoscar 的回复:]
IRichTextString richTextString = cell.RichStringCellValue;
for(int i = 0; i < richTextString.Length; ++i)
{
    IFont font = workbook.GetFontAt(richTextString.GetFontAtIndex(i));
    if(font.IsStrikeout)
         //位于i上的字符是带删除线的
}
这个是针对Office 2003 的版本的吧 Office 2007好像不行[/quote] 那就贴出你的代码来 上面写的都是和版本无关的代码,你所谓的2007好像不行纯粹是你自己的问题
Simeon8888 2016-04-25
  • 打赏
  • 举报
回复
引用 5 楼 lzwdlut 的回复:
Advantage of NPOI a. It's totally free to use b. Cover most features of Excel (cell style, data format, formula and so on) c. Support xls, xlsx, docx. d. Designed to be interface-oriented (take a look at NPOI.SS namespace) e. Support not only export but also import f. .Net 2.0 based even for xlsx and docx (though we also support .NET 4.0) g. Successful cases from all over the world h. huge amout of basic examples i. No dependency on isolated storage 看网站上写的,最新版肯定支持2007格式的了。不过没用过这个东西。
不支持,不能读取我需要的东西
技道两进 2016-04-18
  • 打赏
  • 举报
回复
Advantage of NPOI a. It's totally free to use b. Cover most features of Excel (cell style, data format, formula and so on) c. Support xls, xlsx, docx. d. Designed to be interface-oriented (take a look at NPOI.SS namespace) e. Support not only export but also import f. .Net 2.0 based even for xlsx and docx (though we also support .NET 4.0) g. Successful cases from all over the world h. huge amout of basic examples i. No dependency on isolated storage 看网站上写的,最新版肯定支持2007格式的了。不过没用过这个东西。
Simeon8888 2016-04-17
  • 打赏
  • 举报
回复
引用 2 楼 shingoscar 的回复:
IRichTextString richTextString = cell.RichStringCellValue;
for(int i = 0; i < richTextString.Length; ++i)
{
    IFont font = workbook.GetFontAt(richTextString.GetFontAtIndex(i));
    if(font.IsStrikeout)
         //位于i上的字符是带删除线的
}
这个是针对Office 2003 的版本的吧 Office 2007好像不行
Simeon8888 2016-04-17
  • 打赏
  • 举报
回复
上面 这个是可以用的 但是对于这种只有一行的 就没用了
Poopaye 2016-04-15
  • 打赏
  • 举报
回复
IRichTextString richTextString = cell.RichStringCellValue;
for(int i = 0; i < richTextString.Length; ++i)
{
    IFont font = workbook.GetFontAt(richTextString.GetFontAtIndex(i));
    if(font.IsStrikeout)
         //位于i上的字符是带删除线的
}
RubenLuLu 2016-04-15
  • 打赏
  • 举报
回复
不使用NPOI我倒是会。然而NPOI我没接触过

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧