在c#WinForm中,引用using Microsoft.Office.Interop.Excel 出错 20
在c#WinForm中,引用usingMicrosoft.Office.Interop.Excel运行出错:命名空间“Microsoft”中不存在类型或命名空间名称“Of...
在c#WinForm中,引用using Microsoft.Office.Interop.Excel 运行出错:命名空间“Microsoft”中不存在类型或命名空间名称“Office”
这是为什么?哪位大哥知道原因的说下,不胜感激! 展开
这是为什么?哪位大哥知道原因的说下,不胜感激! 展开
展开全部
在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" );
}
}
希望 可以帮上你的忙!
添加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" );
}
}
希望 可以帮上你的忙!
展开全部
确认你的项目--引用中有没有Microsoft.Office.Interop.Excel 的引用。如果没有,先添加dll引用:“引用”右键-->“添加引用”-->.NET选项卡。找到Microsoft.Office.Interop.Excel。 如果没有这个dll,那得先装个office吧。如果你的是vs2008应该是带这些office的dll的。你在找找看。 最后在使用using Microsoft.Office.Interop.Excel 在代码中引用。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
引用C:\Program Files\Microsoft Office\OFFICE11\Excel.exe就可以了。
将WinForm中的DataGridView导入Excel代码
#region 导入EXCEL
/// <summary>
/// 导入到Excel文件中
/// </summary>将DataGridView中的数据导入到Excel中
/// <param name="mysql"></param>参数为datagridview
public void ExportDataGridViewToExcel(DataGridView myDataGridView, string ReportTitle)
{
System.Data.DataTable mydt = (System.Data.DataTable)myDataGridView.DataSource;//////取得dataGrid绑定的DataSet
try
{
Excel.Application xlApp = new Excel.ApplicationClass();
int rowIndex;
int colIndex;
rowIndex = 2;
colIndex = 0;
Excel.Workbook xlbook = xlApp.Workbooks.Add(true);
if (myDataGridView.Rows.Count > 0)
{
Excel.Range range = xlApp.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, myDataGridView.Columns.Count]);
range.MergeCells = true;
xlApp.ActiveCell.FormulaR1C1 = ReportTitle;
xlApp.ActiveCell.Font.Size = 18;
xlApp.ActiveCell.Font.Bold = true;
foreach (DataGridViewColumn colu in myDataGridView.Columns)
{
colIndex = colIndex + 1;
xlApp.Cells[2, colIndex] = colu.HeaderText;
}//得到的表所有行,赋值给单元格
for (int row = 0; row < mydt.Rows.Count; row++)
{
rowIndex = rowIndex + 1;
colIndex = 0;
for (int col = 0; col < myDataGridView.Columns.Count; col++)
{
colIndex = colIndex + 1;
xlApp.Cells[rowIndex, colIndex] = myDataGridView.Rows[row].Cells[col].Value;
}
}
}
else
{
Excel.Range range = xlApp.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, mydt.Columns.Count]);
range.MergeCells = true;
xlApp.ActiveCell.FormulaR1C1 = ReportTitle;
xlApp.ActiveCell.Font.Size = 18;
xlApp.ActiveCell.Font.Bold = true;
//将表中的栏位名称填到Excel的第一行
foreach (DataColumn Col in mydt.Columns)
{
colIndex = colIndex + 1;
xlApp.Cells[2, colIndex] = Col.ColumnName;
}
//得到的表所有行,赋值给单元格
for (int row = 0; row < mydt.Rows.Count; row++)
{
rowIndex = rowIndex + 1;
colIndex = 0;
for (int col = 0; col < mydt.Columns.Count; col++)
{
colIndex = colIndex + 1;
xlApp.Cells[rowIndex, colIndex] = myDataGridView.Rows[row].Cells[col].Value;
}
}
}
xlApp.get_Range(xlApp.Cells[2, 1], xlApp.Cells[2, colIndex]).Font.Bold = true;
xlApp.get_Range(xlApp.Cells[2, 1], xlApp.Cells[rowIndex, colIndex]).Borders.LineStyle = 1;
xlApp.Cells.EntireColumn.AutoFit();
xlApp.Cells.VerticalAlignment =Excel.Constants.xlCenter;
xlApp.Cells.HorizontalAlignment =Excel.Constants.xlCenter;
xlApp.Visible = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
#endregion
private void button2_Click(object sender, EventArgs e)
{
ExportDataGridViewToExcel(dataGridView1, "");
}
将WinForm中的DataGridView导入Excel代码
#region 导入EXCEL
/// <summary>
/// 导入到Excel文件中
/// </summary>将DataGridView中的数据导入到Excel中
/// <param name="mysql"></param>参数为datagridview
public void ExportDataGridViewToExcel(DataGridView myDataGridView, string ReportTitle)
{
System.Data.DataTable mydt = (System.Data.DataTable)myDataGridView.DataSource;//////取得dataGrid绑定的DataSet
try
{
Excel.Application xlApp = new Excel.ApplicationClass();
int rowIndex;
int colIndex;
rowIndex = 2;
colIndex = 0;
Excel.Workbook xlbook = xlApp.Workbooks.Add(true);
if (myDataGridView.Rows.Count > 0)
{
Excel.Range range = xlApp.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, myDataGridView.Columns.Count]);
range.MergeCells = true;
xlApp.ActiveCell.FormulaR1C1 = ReportTitle;
xlApp.ActiveCell.Font.Size = 18;
xlApp.ActiveCell.Font.Bold = true;
foreach (DataGridViewColumn colu in myDataGridView.Columns)
{
colIndex = colIndex + 1;
xlApp.Cells[2, colIndex] = colu.HeaderText;
}//得到的表所有行,赋值给单元格
for (int row = 0; row < mydt.Rows.Count; row++)
{
rowIndex = rowIndex + 1;
colIndex = 0;
for (int col = 0; col < myDataGridView.Columns.Count; col++)
{
colIndex = colIndex + 1;
xlApp.Cells[rowIndex, colIndex] = myDataGridView.Rows[row].Cells[col].Value;
}
}
}
else
{
Excel.Range range = xlApp.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, mydt.Columns.Count]);
range.MergeCells = true;
xlApp.ActiveCell.FormulaR1C1 = ReportTitle;
xlApp.ActiveCell.Font.Size = 18;
xlApp.ActiveCell.Font.Bold = true;
//将表中的栏位名称填到Excel的第一行
foreach (DataColumn Col in mydt.Columns)
{
colIndex = colIndex + 1;
xlApp.Cells[2, colIndex] = Col.ColumnName;
}
//得到的表所有行,赋值给单元格
for (int row = 0; row < mydt.Rows.Count; row++)
{
rowIndex = rowIndex + 1;
colIndex = 0;
for (int col = 0; col < mydt.Columns.Count; col++)
{
colIndex = colIndex + 1;
xlApp.Cells[rowIndex, colIndex] = myDataGridView.Rows[row].Cells[col].Value;
}
}
}
xlApp.get_Range(xlApp.Cells[2, 1], xlApp.Cells[2, colIndex]).Font.Bold = true;
xlApp.get_Range(xlApp.Cells[2, 1], xlApp.Cells[rowIndex, colIndex]).Borders.LineStyle = 1;
xlApp.Cells.EntireColumn.AutoFit();
xlApp.Cells.VerticalAlignment =Excel.Constants.xlCenter;
xlApp.Cells.HorizontalAlignment =Excel.Constants.xlCenter;
xlApp.Visible = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
#endregion
private void button2_Click(object sender, EventArgs e)
{
ExportDataGridViewToExcel(dataGridView1, "");
}
参考资料: http://hi.baidu.com/czyblues/blog/item/1f8d9eca48e8b747f21fe7c7.html
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
安装一个Office的MSI就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询