乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > <em>VS2010</em>环境下 C++<em>读取excel</em&g

<em>VS2010</em>环境下 C++<em>读取excel</em&g

作者:乔山办公网日期:

返回目录:excel表格制作


读取Excel首先你在e799bee5baa6e997aee7ad94e58685e5aeb9333引用中得引用Microsoft office Excel 20** 对应的dll文件,才能读取到Excel的数据
在类里面引用:System.Data.OleDb;

static public DataSet ExcelToDataSet(string filename)
{
DataSet ds;
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Extended Properties=Excel 8.0;" +
"data source=" + filename;
OleDbConnection myConn = new OleDbConnection(strCon);
string strCom = " SELECT * FROM [Sheet1$]";
myConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
ds = new DataSet();
myCommand.Fill(ds);
myConn.Close();
return ds;
}
这个方法返回一个数据读取的数据原给你的datagridview

如果Excel表格里的数据都是格式化好的,推荐使用 ODBC来访问,可以完全把Excel文件当作数据库,然后进行增删改查,这种方法很常用。

1.使用函数xlsread读取单个文件
(1)num=xlsread(filename)
(2)num = xlsread(filename, -1)
(3)num = xlsread(filename, sheet)
(4)num = xlsread(filename,sheet, 'range')

例如:

将读取D盘下的一个文件路径为:D:/test/daxia.xls
num = xlsread('d:/test/daxia.xls',2,'D2:H4')读取sheet2的D2:H4区域数据
如果要进行交互式的选择用:num = xlsread('d:/test/daxia.xls',-1)


2.批量的文件要读取
首先将多个文件进行批量重命名zd
然后用循环的方式实现:
for i=1:100
filename=['d:/test/daxia',num2str(i),'.xls']; 
num=xlsread(filename)
end



你指的是excel 2013中的表格吗?如果是单纯的表格的话,你可以将excel文件另存为".csv" (comma-sperated-version, 中文为逗号分隔)格式,e5a48de588b67a686964616f339每个逗号对应的就是表格的一个单元。例如

// Example.csv
Name, Age, ID
David, 23, 0

就是一个2乘2的表格用.csv的形式来表示的,用C++读入这样的csv文件就可以了,代码如下:

void readCSV(const char* fileName, vector<vector<string>>& csvVector)
{
ifstream file(fileName);

while (file)
{
string s;
if (!getline(file, s)) break;

istringstream ss(s);
vector <string> record;

while (ss)
{
string s;
if (!getline(ss, s, ',')) break;
record.push_back(s);
}

csvVector.push_back(record);
}
if (!file.eof())
{
cerr << "Fooey!\n";
}
}

参数中的csvVector是一个二维的vector, 分别代表行和列,每次按行读入。

相关阅读

  • 在excel文档里如何去掉背景颜色

  • 乔山办公网excel表格制作
  • 桌面->右键->属性->外观->高级->项目选择(窗口)、颜抄色1(L)选择(其它)将色调改为:85。饱和度:123。亮度:205->添加百到自定义颜色->在自定义颜色选定点确定->确定这样所
关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网