你是要从excel中导入数据到winfor" />
乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > C# <em>winform</em> 中如何<em>导入Excel</em&

C# <em>winform</em> 中如何<em>导入Excel</em&

作者:乔山办公网日期:

返回目录:excel表格制作


Range.Value2 = datarow[0].value

你是要从excel中导入数据到winform吗?如e69da5e6ba90e79fa5e98193332果是这样,可以这样:引用office11.0组件后, Microsoft.Office.Interop.Excel.Application application; //这是一个客户端
Microsoft.Office.Interop.Excel.Workbooks workbooks; //所有工作薄
Microsoft.Office.Interop.Excel.Worksheet worksheet;//工作表
Microsoft.Office.Interop.Excel.Workbook workbook; //所用到的工作表 void IsRunEX(){ OpenFileDialog openfilediaglog = new OpenFileDialog();
openfilediaglog.Filter = "xls文件|*.xls";
if (openfilediaglog.ShowDialog() == DialogResult.OK)
{
FieldName = openfilediaglog.FileName;
application = new Microsoft.Office.Interop.Excel.Application();
workbooks = application.Workbooks;
workbook = returnworkbook(FieldName, workbooks);
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1]; //选择第一个表
Range range = worksheet.Cells[1, 8] as Range; //这是选择第一行第八列的内容
Range rangee = worksheet.Cells[1, 9] as Range; //这是第一行到九列的内容 string str1=range.Value2.ToString(); string str2=ragee.Value2.ToString(); ................... //你所要做的操作 .................. workbook.Close(Type.Missing, FieldName, Type.Missing);
workbooks.Close(); //退出关闭资源
application.Quit();
}
} private Workbook returnworkbook(string filename,Workbooks works) //这里是打一开一个工作表
{
Microsoft.Office.Interop.Excel.Workbook wk=works.Open(
filename, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
return wk;
}
在Visual Studio .NET中建立一个C# WinForm工程.
添加Microsoft Excel Object Library引用:
右键单击Project , 选“添加引用”
在COM 标签项,选中 locate Microsoft Excel Object Library
点确定按钮完成添加引用。 On the View menu, select Toolbox to display the Toolbox. Add two buttons and a check box to Form1.
在Form1上添加一个button1,双击 Button1,添加click事件的代码.把数组里的数据填到Excel表格。
首先添加引用:

using System.Reflection;
using Excel = Microsoft.office.Interop.Excel;

声明两个类的成员变量
Excel.Application objApp;
Excel._Workbook objBook;

private void button1_Click(object sender, System.EventArgs e)
{
Excel.Workbooks objBooks;
Excel.Sheets objSheets;
Excel._Worksheet objSheet;
Excel.Range range;

try
{
// Instantiate Excel and start a new workbook.
objApp = new Excel.Application();
objBooks = objApp.Workbooks;
objBook = objBooks.Add( Missing.Value );
objSheets = objBook.Worksheets;
objSheet = (Excel._Worksheet)objSheets.get_Item(1);

//Get the range where the starting cell has the address
//m_sStartingCell and its dimensions are m_iNumRows x m_iNumCols.
range = objSheet.get_Range("A1", Missing.Value);
range = range.get_Resize(5, 5);

if (this.FillWithStrings.Checked == false)
{
//Create an array.
double[,] saRet = new double[5, 5];

//Fill the array.
for (long iRow = 0; iRow < 5; iRow++)
{
for (long iCol = 0; iCol < 5; iCol++)
{
//Put a counter in the cell.
saRet[iRow, iCol] = iRow * iCol;
}
}

//Set the range value to the array.
range.set_Value(Missing.Value, saRet );
}

else
{
//Create an array.
string[,] saRet = new string[5, 5];

//Fill the array.
for (long iRow = 0; iRow < 5; iRow++)
{
for (long iCol = 0; iCol < 5; iCol++)
{
//Put the row and column address in the cell.
saRet[iRow, iCol] = iRow.ToString() + "|" + iCol.ToString();
}
}

//Set the range value to the array.
range.set_Value(Missing.Value, saRet );
}

//Return control of Excel to the user.
objApp.Visible = true;
objApp.UserControl = true;
}
catch( Exception theException )
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat( errorMessage, theException.Message );
errorMessage = String.Concat( errorMessage, " Line: " );
errorMessage = String.Concat( errorMessage, theException.Source );

MessageBox.Show( errorMessage, "Error" );
}
}

希望 可以帮上你的忙7a686964616fe58685e5aeb9366

给你个详细点的吧。。
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" EXCEL的本地路径 ";Extended Properties=Excel 8.0;");
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
strExcel = string.Format("select '号码e79fa5e98193e59b9ee7ad94334' from [这里填你的EXCEL左下角的工作表名$]");
myCommand = new OleDbDataAdapter(strExcel,conn);
DataTable dt=new DataTable();
myCommand .Fill(dt);//把所有结果填充到dt里面
string rusult="":
for(int i=0;i<dt.Row.count;i++)
{
rusult+=dt.Rows[i][0].ToString()+";";//遍历所有号码相加
}
textbox1.Text=rusult;

相关阅读

关键词不能为空
极力推荐

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