[英]Importing excel data to datagrid using interop C#
I have just started on importing excel data to datagrid.I found a code and tried in on my system but I'm net getting anything in the datagrid.Can anyone please tell me what I'm doing wrong here. 我刚开始将excel数据导入到datagrid中。我找到了一个代码并在系统上进行了尝试,但是我在datagrid中得到了任何东西,有人可以告诉我我在这里做错了什么。 Also if anyone has a working code can u please share it with me.
另外,如果任何人都有有效的代码,您可以与我分享。 Here is my code
这是我的代码
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ArrayList test = ProcessWorkbook("C:\\Users\\s_kamalaksha_prabhu\\Desktop\\Book1.xlsx");
if (test != null)
dataGridView1.DataSource = test;
}
public ArrayList ProcessWorkbook(string filePath)
{
string file = filePath;
Microsoft.Office.Interop.Excel.Application excel = null;
Microsoft.Office.Interop.Excel.Workbook wkb = null;
ArrayList al = new ArrayList();
try
{
excel = new Microsoft.Office.Interop.Excel.Application();
wkb = ExcelTools.OpenBook(excel, file, false, true, false);
Microsoft.Office.Interop.Excel.Worksheet sheet = wkb.Sheets["Employees$"] as Microsoft.Office.Interop.Excel.Worksheet;
Microsoft.Office.Interop.Excel.Range range = null;
if (sheet != null)
range = sheet.get_Range("A1:X6702", System.Type.Missing);
if (range != null)
{
foreach (Microsoft.Office.Interop.Excel.Range r in range)
{
al.Add(r.Text);
}
}
}
catch (Exception ex)
{
//if you need to handle stuff
Console.WriteLine(ex.Message);
}
finally
{
if (wkb != null)
ExcelTools.ReleaseRCM(wkb);
if (excel != null)
ExcelTools.ReleaseRCM(excel);
}
return al;
}
//----------------
public static class ExcelTools
{
public static Microsoft.Office.Interop.Excel.Workbook OpenBook(Microsoft.Office.Interop.Excel.Application excelInstance, string fileName, bool readOnly, bool editable,
bool updateLinks)
{
Microsoft.Office.Interop.Excel.Workbook book = excelInstance.Workbooks.Open(
fileName, updateLinks, readOnly,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, editable, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
return book;
}
public static void ReleaseRCM(object o)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
}
catch
{
}
finally
{
o = null;
}
}
}
}
}
Thanks. 谢谢。
I don't know what this is supposed to do: 我不知道这应该做什么:
foreach (Microsoft.Office.Interop.Excel.Range r in range)
{
al.Add(r.Text);
}
If you want to go through the cells use range.Cells. 如果要遍历单元格,请使用range.Cells。 For rows use range.Rows I guess if you want to fill a grid you'll want to do something like:
对于行,请使用range.Rows。我想如果要填充网格,则需要执行以下操作:
foreach (Microsoft.Office.Interop.Excel.Range row in range.Rows)
{
// Add a new row
foreach (Microsoft.Office.Interop.Excel.Range cell in row.Cells)
{
// Write to the cells' column on the current row
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.