惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Security @ Cisco Blogs
T
Threat Research - Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
腾讯CDC
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
罗磊的独立博客
F
Full Disclosure
博客园 - 【当耐特】
C
CERT Recently Published Vulnerability Notes
Engineering at Meta
Engineering at Meta
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Threatpost
I
Intezer
V2EX - 技术
V2EX - 技术
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Hacker News
The Hacker News
小众软件
小众软件
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
N
News | PayPal Newsroom
MyScale Blog
MyScale Blog
AI
AI
Vercel News
Vercel News
Spread Privacy
Spread Privacy
美团技术团队
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
V
Vulnerabilities – Threatpost
Schneier on Security
Schneier on Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
Help Net Security
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
L
LINUX DO - 热门话题
U
Unit 42
L
LangChain Blog
Recent Announcements
Recent Announcements

博客园 - phcis

c# 全角(SBC)和半角(DBC)相互转换函数 用HttpWebRequest做POST请求时返回Http 417 错误解决方法 使用webbroswer的一点技巧记录 计算字符串相似度及寻找最相似字符串的代码 asp.net中js返回false时阻止form提交的方法 【转】DIV+CSS 让文字居中于背景图 sql 语句实现分页 【转】C#产生随机字符的两段代码 【转】datagridview的checkbox列,当修改checkbox状态时实时获得其准确状态值 由于使用“优易U盘加密软件”导致电脑无法关机/蓝屏等解决方法 批量删除数据库中所有表的记录(清空数据库) C# HttpRequest基础连接已经关闭: 接收时发生意外错误 GridView 动态绑定数据,包括2个或者多个值 - phcis - 博客园 近期动向 使用ajax导致滚动条复位的解决方法 AT编程常见问题与错误代码的意义 c# 发送email,正文支持html格式,包含附件 使用AutocompleteExtender无效或者没反应的原因记录 使用 DateTimePicker 控件显示和选择时间
c# 读取excel的一系列问题 - phcis - 博客园
phcis · 2010-11-19 · via 博客园 - phcis

c#读取excel的方法有不少,这里重点讲直接将excel文件读取到dataset中。在实践过程中会有不少问题,特意摘抄在此:

1.基本的代码

 private DataSet importExcelToDataSet(string FilePath)
        {
            
string strConn;
            strConn 
= "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + FilePath + ";Extended Properties='Excel 8.0;HDR=No;IMEX=1'";
            OleDbConnection conn 
= new OleDbConnection(strConn);
           
// OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [出车登记表$A:K]", strConn);
            OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [出车登记表$A:L]  where f3<>''", strConn);
            DataSet myDataSet 
= new DataSet();
            
try
            {
                myCommand.Fill(myDataSet);
            }
            
catch (Exception ex)
            {
                MessageBox.Show(
"error," + ex.Message);
            }
            
return myDataSet;
        }

调用的时候传入参数为excel文件的路径,返回值是一个dataset。

2.代码注意事项

2.1这一句“ strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + FilePath + ";Extended Properties='Excel 8.0;HDR=No;IMEX=1'";
”。

2.1.1:Extended Properties后面的值,excel的版本:excel2003/07都要指定为8.0

2.1.2:HDR=No;表示是否将excle中的第一行也作为数据读取到dataset中

2.1.3:IMEX=1 表示将某列中既有数值又有文本的统一认为是文本处理,此语句可解决某些单元格内容读取到excel中是空白的问题。

2.2这一句“ OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [出车登记表$A:L]  where f3<>''", strConn);

2.2.1:SELECT * FROM  谁都知道是干什么的,后面跟上"[]",里面写要读取的工作表的名字,以美元符号结束,如“工作表1$”这样。

2.2.2:如果要读取指定范围内的列,则在工作表名字以及美元符号后写上起始列编号+冒号+截止列编号,如“[工作表1$C:K]”。此语句可解决读取过程中出现“包含了太多列”的异常。

2.2.3如果某些单元格内容为空就不读取此行则增加where条件,假设工作表中的A列中某一个单元格内容为空不读取则写“ WHERE f1<>''”,如果是b列则是f2<>''依此类推。此语句可解决读取完成的datast中包含n多无意义的空行。其他情况需要自行发现了。