c# 32位机和64位机 读取Excel内容到DataSet
// ----------------------32位机 //注释说明 //ExclePath 为Excel路径 批号 是指Excel文件中某一列必填项
public static DataSet GetDataTableForExcel(String ExclePath)
{
string strCon = String.Empty;
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ExclePath + "; Extended Properties='Excel 8.0;IMEX=1';";
OleDbConnection olecon = new OleDbConnection(strCon);
olecon.Open();
DataTable dt = olecon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string tableName = dt.Rows[][].ToString().Trim();
OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [" + tableName + "] Where 批号 is not null", strCon);// Where 条件根据实际情况进行更改
DataSet myds = new DataSet();
try
{
myda.Fill(myds);
}
catch { myds = null; }
olecon.Close();
return myds;
} //-----------------------------64位机 /// <summary>
/// 把Excel里的数据转换为DataTable,应用引用的com组件:Microsoft.Office.Interop.Excel.dll 读取EXCEL文件
/// </summary>
/// <param name="filenameurl">物理路径</param>
/// <param name="sheetIndex">sheet名称的索引</param>
/// <param name="splitstr">如果是已存在列,则自定义添加的字符串</param>
/// <returns></returns>
public static DataTable ExecleToDataSet(string filenameurl, int sheetIndex, string splitstr)
{
//
Microsoft.Office.Interop.Excel.Workbook wb = null;
Microsoft.Office.Interop.Excel.Worksheet ws = null;
bool isEqual = false;//不相等
ArrayList columnArr = new ArrayList();//列字段表
DataSet myDs = new DataSet();
DataTable xlsTable = myDs.Tables.Add("show");
object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();//lauch excel application
if (excel != null)
{
excel.Visible = false;
excel.UserControl = true;
// 以只读的形式打开EXCEL文件
wb = excel.Workbooks.Open(filenameurl, missing, true, missing, missing, missing,
missing, missing, missing, true, missing, missing, missing, missing, missing);
//取得第一个工作薄
ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.get_Item(sheetIndex);
//取得总记录行数(包括标题列)
int rowsint = ws.UsedRange.Cells.Rows.Count; //得到行数
int columnsint = ws.UsedRange.Cells.Columns.Count;//得到列数
DataRow dr;
for (int i = ; i <= columnsint; i++)
{
//判断是否有列相同
if (i >= )
{
int r = ;
for (int k = ; k <= i - ; k++)//列从第一列到第i-1列遍历进行比较
{
if (((Microsoft.Office.Interop.Excel.Range)ws.Cells[, i]).Text.ToString() == ((Microsoft.Office.Interop.Excel.Range)ws.Cells[, k]).Text.ToString())
{
//如果该列的值等于前面列中某一列的值
xlsTable.Columns.Add(((Microsoft.Office.Interop.Excel.Range)ws.Cells[, i]).Text.ToString() + splitstr + (r + ).ToString(), typeof(string));
columnArr.Add(((Microsoft.Office.Interop.Excel.Range)ws.Cells[, i]).Text.ToString() + splitstr + (r + ).ToString());
isEqual = true;
r++;
break;
}
else
{
isEqual = false;
continue;
}
}
if (!isEqual)
{
xlsTable.Columns.Add(((Microsoft.Office.Interop.Excel.Range)ws.Cells[, i]).Text.ToString(), typeof(string));
columnArr.Add(((Microsoft.Office.Interop.Excel.Range)ws.Cells[, i]).Text.ToString());
}
}
else
{
xlsTable.Columns.Add(((Microsoft.Office.Interop.Excel.Range)ws.Cells[, i]).Text.ToString(), typeof(string));
columnArr.Add(((Microsoft.Office.Interop.Excel.Range)ws.Cells[, i]).Text.ToString());
}
}
for (int i = ; i <= rowsint; i++)
{
dr = xlsTable.NewRow();
for (int j = ; j <= columnsint; j++)
{
dr[columnArr[j - ].ToString()] = ((Microsoft.Office.Interop.Excel.Range)ws.Cells[i, j]).Text.ToString();
}
xlsTable.Rows.Add(dr);
}
}
excel.Quit();
excel = null;
Dispose(ws, wb);
return xlsTable;
}
c# 32位机和64位机 读取Excel内容到DataSet的更多相关文章
- C# 32位程序访问64位系统注册表
原文:C# 32位程序访问64位系统注册表 我的上一篇文章已经阐述了“32位程序和64位程序在64位平台上读\写注册表的区别”,那么接下来将要回答上篇所留下来的一个问题:32位程序如何访问64位系统注 ...
- 【电脑常识】如何查看电脑是32位(X86)还是64位(X64),如何知道硬件是否支持64位系统
开始->运行->输入cmd确定->输入systeminfo 回车 待加载完成,就会看到如下信息(不同版本略有差异): 一.如何查看电脑是32位(X86)还是64位(X64) 方法2: ...
- Visual Studio远程调试监视器(MSVSMON.EXE)的32位版本不能用于调试64位进程或64位转储
在VS2013中调试Silverlight项目时,提示:无法附加.Visual Studio远程调试监视器(MSVSMON.EXE)的32位版本不能用于调试64位进程或64位转储.请改用64位版本. ...
- iOS上应用如何兼容32位系统和64位系统
在苹果推出iPhone5S时,64位的应用就走到了眼前.当时就看见苹果官方资料宣布iOS7.x的SDK支持了64位的应用,而且内置的应用都已经是64位. 我记得自己刚刚接触电脑时还有16位的系统,指针 ...
- 让32位Eclipse和64位Eclipse同时在64的Windows7上运行
转自让32位Eclipse和64位Eclipse同时在64的Windows7上运行 参考这篇文章:http://wenku.baidu.com/view/57994c270066f5335a81214 ...
- C# 第三方DLL,可以实现PDF转图片,支持32位系统、64位系统
itextsharp.dll,是一个开源的在C#中用来生成PDF文档的库文件,不少C#爱好者用它制作出了PDF文档生成器.使用时只需在你的C#项目中添加引入此组件即可,使用方法网上有很多,自己查阅一下 ...
- X86(32位)与X64(64位)有什么区别,如何选择对应的操作系统和应用程序?
X86就是我们一般用的32位的系统,指针长度为32位(386起):X64就是64位的系统,指针长度为64位. 选择硬件对应的软件,建议通过以下三条考虑:1.64位操作系统相对32位操作系统理论上性能会 ...
- 32位程序在64位系统上获取系统安装时间(要使用KEY_WOW64_64KEY标记)
众所周知,取系统的安装时间可取注册表HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion的子项InstallDate,此值是个 ...
- 使用.netFx4.0提供的方法解决32位程序访问64位系统的64位注册表
原文:使用.netFx4.0提供的方法解决32位程序访问64位系统的64位注册表 我们知道目标平台是32位的程序运行在64位的系统上,去访问部分注册表的时候系统自动重定向到win32node节点对应的 ...
随机推荐
- POJ-1861,Network,最小生成树水题,,注意题面输出有问题,不必理会~~
Network Time Limit: 1000MS Memory Limit: 30000K Special Judge http://poj.org/problem?id=1 ...
- CDQ分治模板
#include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #defi ...
- 跨多种环境部署 Gearman -改善应用程序性能和降低服务器负载
您可能想要将工作扩散到一个大型机器群体中,或者想要在不同语言和环境之间共享功能,那么开放源码的 Gearman 服务可以让您轻松地将工作分布到网络中的其他机器.本文将介绍 Gearman 的一些典型应 ...
- HDU 2059 【DP】
题意: 中文. 思路: 这题不是自己的思想. 当对第i个点的最优值进行求解的时候一定存在最后一个加油的点j.这里j直接枚举. 另外将0和n+1个加油站定义为起点和终点. dp需要加强训练. #incl ...
- easyui combotree选项重复
现象 编辑,赋值出现重复选项 原因 值之间有空格,比如我取值是3, 4, 6要改成3,4,6 注意:数值之间的空格去掉了
- [转]Wireshark抓包工具--TCP数据包seq ack等解读
原文: http://blog.csdn.net/wang7dao/article/details/16805337/ ---------------------------------------- ...
- No module named '_sqlite3'问题解决
Centos自带的python版本是2.7的,后面我自己装了3.5版本的,在创建应用的时候python manager.py startapp users 时,就会报No module named ' ...
- [React] Extend styles with styled-components in React
In this lesson, you will learn how to extend styles from one styled-component to another in a React ...
- Win7 Windows Update更新的文件默认在哪个位置
C:\Windows\SoftwareDistribution\download
- VC++ 2010编译错误 fatal error C1189 error This file requires _WIN32_WINNT to be #defined at least
打开你的C++工程,找到里面的stdafx.h文件,然后把下面的红色内容替换成绿色的 参考:http://blog.csdn.net/dongliqiang2006/article/details/5 ...