乔山办公网我们一直在努力
您的位置:乔山办公网 > office365 > C#的winform窗体引用 Microsoft.Offic...

C#的winform窗体引用 Microsoft.Offic...

作者:乔山办公网日期:

返回目录:office365


首先要在引用中添加COM组件,然后看看添加了命名空间using Microsoft.office.interop.excel没

直接给你例子参考
#region 写入Excel文档
/// <summary>
/// 写入Excel文档
/// </summary>
/// <param name="Path">文件名称</param>
/// <param name="ds2Excel">被导出的数据集</param>

public static void SaveFP2toExcel(string Path, DataSet ds2Excel)
{
try
{
//创建Excel应用程序e799bee5baa6e79fa5e98193e59b9ee7ad94365

Excel.Application xApp = new Excel.ApplicationClass();
if (xApp == null)
{
MessageBox.Show("错误:Excel不能打开!");
return;
}
object objOpt = Missing.Value;
Excel.Workbook xBook = xApp.Workbooks.Add(true);//添加新工作簿
Excel.Sheets xSheets = xBook.Sheets;
Excel._Worksheet xSheet = null;
//
//创建空的sheet
//
xSheet = (Excel._Worksheet)(xBook.Sheets.Add(objOpt, objOpt, objOpt, objOpt));
xSheet.Cells.NumberFormatLocal = "@";
if (xSheet == null)
{
MessageBox.Show("错误:工作表为空!");
return;
}
//写数据集表头
for (int k = 0; k < ds2Excel.Tables[0].Columns.Count; k++)
xSheet.Cells[1, k + 1] = ds2Excel.Tables[0].Columns[k].ColumnName.ToString().Trim();
//写数据集数据
for (int i = 0; i < ds2Excel.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < ds2Excel.Tables[0].Columns.Count; j++)
xSheet.Cells[i + 2, j + 1] = ds2Excel.Tables[0].Rows[i][j].ToString();
}
//保存文件
xBook.Saved = true;
xBook.SaveCopyAs(Path);

//显示文件
xApp.Visible = true;
//
//释放资源
//
Marshal.ReleaseComObject(xSheet);
xSheet = null;
Marshal.ReleaseComObject(xSheets);
xSheets = null;

Marshal.ReleaseComObject(xBook);
xBook = null;
xApp.Quit();
Marshal.ReleaseComObject(xApp);
xApp = null;
GC.Collect();//强行销毁
MessageBox.Show("导出成功");

}
catch (Exception ex)
{
MessageBox.Show("写入Excel发生错误:" + ex.Message);
}
}
#endregion
在控制面板中,选择“添加删除程序”,找到office ,选择“更改”,在对话框中选择“添加删除功能”,然后选择自定义安装,添加上office工具 中的.net可编程性支持,安全起见,把excel下的,word下的此选项都添加上,然后点击“更新”按钮,更新office,完成后再回到.net开 发平台,OK!

在Visual Studio .NET中建立一个C# WinForm工程.
添加Microsoft Excel Object Library引用:
右键单击Project , 选“添加引e79fa5e98193e59b9ee7ad94366用”
在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" );
}
}

希望 可以帮上你的忙!

相关阅读

  • C#的winform窗体引用 Microsoft.Offic...

  • 乔山办公网office365
  • 首先要在引用中添加COM组件,然后看看添加了命名空间using Microsoft.office.interop.excel没直接给你例子参考 #region 写入Excel文档 /// /// 写入Excel文档 ///
关键词不能为空
极力推荐

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