c#利用Excel直接讀取數(shù)據(jù)到DataGridView
在winform里拖入一個(gè)datagridview控件,跟一個(gè)openfiledialog控件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using Microsoft.Office.Core;
using Excel=Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
using System.Reflection;
namespace Excelproject
{
public partial class ExcelForm : Form
{
private ExcelOperate eo;
private string excelpath;
private Excel.Application excel1;
private Excel.Workbooks wbs = null;
private Excel.Workbook wb = null;
private Excel.Sheets wss;
private Excel.Worksheet ws = null;
private Excel.Range range1 = null;
public ExcelForm()
{
InitializeComponent();
this.excel1 = new Excel.Application();
if (excel1 == null)
{
MessageBox.Show("error");
System.Windows.Forms.Application.Exit();
}
excel1.Visible = true;
}
#region excel文件打開關(guān)閉操作
private void 打開_Click(object sender, EventArgs e)
{
openFileDialog1 = new OpenFileDialog();
openFileDialog1.Title = "打開excel文件";
openFileDialog1.Filter = "excel03文件(*.xls)|*.xls|excel07文件(*.xlsx)|*.xlsx";
openFileDialog1.InitialDirectory = @"C:\Users\Administrator\Desktop";
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//打開文件對話框選擇的文件
excelpath = openFileDialog1.FileName;
eo = new ExcelOperate();
readExcel(excelpath);
}
}
void readExcel(string path)
{
object miss = System.Reflection.Missing.Value;
excel1.UserControl = true;
excel1.DisplayAlerts = false;
excel1.Application.Workbooks.Open(excelpath, miss, miss, miss, miss,
miss, miss, miss, miss,
miss, miss, miss, miss,
miss, miss);
wbs = excel1.Workbooks;
wss = wbs[1].Worksheets;
ws = (Excel.Worksheet) wss.get_Item(1);
int rowNum = ws.UsedRange.Cells.Rows.Count;
int colNum = ws.UsedRange.Cells.Columns.Count;
string cellStr = null;
char ch = 'A';
for (int i = 0; i < colNum; i++)
{
dataGridView1.Columns.Add(i.ToString(), ch.ToString());
dataGridView1.Rows.Add(rowNum);
for (int j = 0; j <rowNum; j++)
{
cellStr = ch.ToString() + (j + 1).ToString();
dataGridView1[i, j].Value = ws.UsedRange.Cells.get_Range(cellStr, miss).Text.ToString();
}
ch++;
}
}
#endregion
}
}
相關(guān)文章
unity實(shí)現(xiàn)按住鼠標(biāo)選取區(qū)域截圖
這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)按住鼠標(biāo)選取區(qū)域截圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
C#實(shí)現(xiàn)動態(tài)數(shù)據(jù)繪圖graphic的方法示例
這篇文章主要介紹了C#實(shí)現(xiàn)動態(tài)數(shù)據(jù)繪圖graphic的方法,結(jié)合實(shí)例形式分析了C#根據(jù)動態(tài)數(shù)據(jù)繪制2D數(shù)據(jù)表格的相關(guān)操作技巧,需要的朋友可以參考下2017-09-09
C#如何將查詢到的數(shù)據(jù)庫里面的數(shù)據(jù)輸出到textbox控件
這篇文章主要介紹了C#如何將查詢到的數(shù)據(jù)庫里面的數(shù)據(jù)輸出到textbox控件問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
關(guān)于C# 5.0 CallerMemberName CallerFilePath CallerLineNumber 在.
本篇文章,小編為大家介紹關(guān)于C# 5.0 CallerMemberName CallerFilePath CallerLineNumber 在.NET4中的使用介紹方法,有需要的朋友可以參考一下2013-04-04
C#通過XML節(jié)點(diǎn)屬性/屬性值讀取寫入XML操作代碼實(shí)例
本文主要介紹C#通過XML節(jié)點(diǎn)屬性、屬性值對XML的讀取,寫入操作,大家參考使用吧2013-11-11
json格式數(shù)據(jù)分析工具PageElement類分享(仿Session寫法)
json格式數(shù)據(jù)分析工具PageElement類分享,可像Session一樣自由獲取Json元素的Key與Value。并可方便與ADO進(jìn)行交互2013-12-12
C#使用第三方組件實(shí)現(xiàn)動態(tài)解析和求值字符串表達(dá)式
這篇文章主要介紹了C#如何使用第三方組件(LambdaParser、DynamicExpresso、Z.Expressions)實(shí)現(xiàn)動態(tài)解析和求值字符串表達(dá)式,感興趣的小伙伴可以跟隨小編一起了解一下2022-06-06

