c# 逐行读取excel .xlsx文件
stringstro="Provider=Microsoft.ACE.OLEDB.12.0;"+"Datasource="+openo+";"+"ExtendedProp...
string stro = "Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data source=" + openo + ";" +
"Extended Properties=Excel 8.0";
OleDbConnection oco = new OleDbConnection(stro);
string str1o = "SELECT * FROM [Sheet1$]";
oco.Open();
OleDbDataAdapter odao = new OleDbDataAdapter(str1o, oco);
DataSet dso = new DataSet();
odao.Fill(dso);
dataGridView1.DataSource = dso.Tables[0];
我现在的一下就读完了 我想像txt的ReadLine一样 一行一行读取怎么写 展开
"Data source=" + openo + ";" +
"Extended Properties=Excel 8.0";
OleDbConnection oco = new OleDbConnection(stro);
string str1o = "SELECT * FROM [Sheet1$]";
oco.Open();
OleDbDataAdapter odao = new OleDbDataAdapter(str1o, oco);
DataSet dso = new DataSet();
odao.Fill(dso);
dataGridView1.DataSource = dso.Tables[0];
我现在的一下就读完了 我想像txt的ReadLine一样 一行一行读取怎么写 展开
3个回答
展开全部
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.IO;
namespace 导入导出excel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DataSet ds = new DataSet(); //全局变量
private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Excel Files|*.xlsx";
if (ofd.ShowDialog() == DialogResult.OK)
{
string filename = ofd.FileName;
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbook;
Microsoft.Office.Interop.Excel.Worksheet worksheet;
object oMissing = System.Reflection.Missing.Value;//相当null
workbook = excel.Workbooks.Open(filename, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
worksheet = (Worksheet)workbook.Worksheets[1];
int rowCount = worksheet.UsedRange.Rows.Count;
int colCount = worksheet.UsedRange.Columns.Count;
Microsoft.Office.Interop.Excel.Range range1;
System.Data.DataTable dt = new System.Data.DataTable();
for (int i = 0; i < colCount; i++)
{
range1 = worksheet.get_Range(worksheet.Cells[1, i + 1], worksheet.Cells[1, i + 1]);
dt.Columns.Add(range1.Value2.ToString());
}
for (int j = 1; j < rowCount; j++)
{
DataRow dr = dt.NewRow();
for (int i = 0; i < colCount; i++)
{
range1 = worksheet.get_Range(worksheet.Cells[j + 1, i + 1], worksheet.Cells[j + 1, i + 1]);
dr = range1.Value2.ToString();
}
dt.Rows.Add(dr);
}
dataGridView1.DataSource = dt;
excel.Quit();
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
展开全部
for(int i=0;i<dso.Tables[0].rows.count;i++)
{-------dso.Tables[0].rows[i]["字段名"].Tostring()}像这样读
-------dso.Tables[0].rows[i]["字段名"].Tostring()
}
像这样读
{-------dso.Tables[0].rows[i]["字段名"].Tostring()}像这样读
-------dso.Tables[0].rows[i]["字段名"].Tostring()
}
像这样读
追问
字段名是什么
追答
是数据库表里面的字段名
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用OleDbDataReader读,不要用OleDbDataAdapter
追问
不会啊 能具体点不
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |