如何在控制台应用程序下,操作execl

edwardliu 2010-08-02 10:46:04
在一个控制台应用程序下,使用C#.需要对一个excel表进行操作,包括点击其中的一个button和读取一些数据。
我在vs2008中,直接用vsto新建一个execl 2007工作簿是可以进行execl的一些操作的。但是如何在控制台应用程序下进行操作呢?小弟实在是没接触过这方面,请大家帮助。
...全文
166 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
edwardliu 2010-08-16
  • 打赏
  • 举报
回复
过了这么久还没人帮忙回答一下么。。。
edwardliu 2010-08-02
  • 打赏
  • 举报
回复
是的,需要点击execl的button控件。
qiuxin425 2010-08-02
  • 打赏
  • 举报
回复
你是想用C#操作Excel里面的控件?
edwardliu 2010-08-02
  • 打赏
  • 举报
回复
可能是我没清楚
我需要控制台应用程序能够点击Execl中已经存在的一个button,等到该button执行以后,进行数据的读取
然后进行程序接下来的操作。
qiuxin425 2010-08-02
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection; // 引用这个才能使用Missing字段
using Excel;

namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
//创建Application对象
Excel.Application xApp = new Excel.ApplicationClass();
string strPath = Application.StartupPath + "\\Sample.xls";//你的模板的路径;

xApp.Visible = true;
//得到WorkBook对象, 可以用两种方式之一: 下面的是打开已有的文件
Excel.Workbook xBook = xApp.Workbooks._Open(@"D:\Sample.xls",
Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

Excel.Worksheet xSheet = (Excel.Worksheet)xBook.Sheets[1];//工作表编号

Excel.Range rng3 = xSheet.get_Range("C6", Missing.Value);
rng3.Value2 = "=MAX(B1:B6)";//C6输出内容为B1-B6最大值,类似操作你的数据

xBook.SaveAs(@"D:\CData.xls",
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value); //重新保存的路径的名称。

}
}
}
edwardliu 2010-08-02
  • 打赏
  • 举报
回复
楼上两位能不能说的详细点。。。。
我这边主要是还没什么思路,而且我主要是需要操作Button。这一块比较头大
qiuxin425 2010-08-02
  • 打赏
  • 举报
回复
//创建Application对象
Excel.Application xApp = new Excel.ApplicationClass();
string strPath = Application.StartupPath + "\\Sample.xls";

xApp.Visible = true;
//得到WorkBook对象, 可以用两种方式之一: 下面的是打开已有的文件
Excel.Workbook xBook = xApp.Workbooks._Open(@"D:\Sample.xls",
Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

Excel.Worksheet xSheet = (Excel.Worksheet)xBook.Sheets[1];
Excel.Worksheet xSheet2 = (Excel.Worksheet)xBook.Sheets[2];

Excel.Range rng3 = xSheet.get_Range("C6", Missing.Value);
rng3.Value2 = "=MAX(B1:B6)";

Excel.Range rng4 = xSheet2.get_Range("C6", Missing.Value);
rng4.Value2 = "=MAX(B1:B6)";

xBook.SaveAs(@"D:\CData.xls",
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
  • 打赏
  • 举报
回复
mark
hyblusea 2010-08-02
  • 打赏
  • 举报
回复

//-----------------------------------------------------------------------
// This file is part of the Microsoft .NET Framework SDK Code Samples.
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//This source code is intended only as a supplement to Microsoft
//Development Tools and/or on-line documentation. See these other
//materials for detailed information regarding Microsoft code samples.
//
//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.
//-----------------------------------------------------------------------
using System;
using System.Reflection; // For Missing.Value and BindingFlags
using System.Runtime.InteropServices; // For COMException
using Microsoft.Office.Interop.Excel;

class AutoExcel {
public static int Main() {

Console.WriteLine ("Creating new Excel.Application");
Application app = new Application();
if (app == null) {
Console.WriteLine("ERROR: EXCEL couldn't be started!");
return 0;
}

Console.WriteLine ("Making application visible");
app.Visible = true;

Console.WriteLine ("Getting the workbooks collection");
Workbooks workbooks = app.Workbooks;

Console.WriteLine ("Adding a new workbook");

_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);

Console.WriteLine ("Getting the worksheets collection");
Sheets sheets = workbook.Worksheets;

_Worksheet worksheet = (_Worksheet) sheets.get_Item(1);
if (worksheet == null) {
Console.WriteLine ("ERROR: worksheet == null");
}

Console.WriteLine ("Setting the value for cell");

// This paragraph puts the value 5 to the cell G1
Range range1 = worksheet.get_Range("G1", Missing.Value);
if (range1 == null) {
Console.WriteLine ("ERROR: range == null");
}
const int nCells = 5;
range1.Value2 = nCells;

// This paragraph sends single dimension array to Excel
Range range2 = worksheet.get_Range("A1", "E1");
int[] array2 = new int [nCells];
for (int i=0; i < array2.GetLength(0); i++) {
array2[i] = i+1;
}
range2.Value2 = array2;

// This paragraph sends two dimension array to Excel
Range range3 = worksheet.get_Range("A2", "E3");
int[,] array3 = new int [2, nCells];
for (int i=0; i < array3.GetLength(0); i++) {
for (int j=0; j < array3.GetLength(1); j++) {
array3[i, j] = i*10 + j;
}
}
range3.Value2 = array3;

// This paragraph reads two dimension array from Excel
Range range4 = worksheet.get_Range("A2", "E3");
Object[,] array4;
array4 = (Object[,])range4.Value2;

for (int i=array4.GetLowerBound(0); i <= array4.GetUpperBound(0); i++) {
for (int j=array4.GetLowerBound(1); j <= array4.GetUpperBound(1); j++) {
if ((double)array4[i, j] != array3[i-1, j-1]) {
Console.WriteLine ("ERROR: Comparison FAILED!");
return 0;
}
}
}

// This paragraph fills two dimension array with points for two curves and sends it to Excel
Range range5 = worksheet.get_Range("A5", "J6");
double[,] array5 = new double[2, 10];
for (int j=0; j < array5.GetLength(1); j++) {
double arg = Math.PI/array5.GetLength(1) * j;
array5[0, j] = Math.Sin(arg);
array5[1, j] = Math.Cos(arg);
}
range5.Value2 = array5;

// The following code draws the chart
range5.Select();
ChartObjects chartobjects = (ChartObjects) worksheet.ChartObjects(Missing.Value);

ChartObject chartobject = (ChartObject) chartobjects.Add(10 /*Left*/, 100 /*Top*/, 450 /*Width*/, 250 /*Height*/);
_Chart chart = (_Chart) chartobject.Chart;

// Call to chart.ChartWizard() is shown using late binding technique solely for the demonstration purposes
Object[] args7 = new Object[11];
args7[0] = range5; // Source
args7[1] = XlChartType.xl3DColumn; // Gallery
args7[2] = Missing.Value; // Format
args7[3] = XlRowCol.xlRows; // PlotBy
args7[4] = 0; // CategoryLabels
args7[5] = 0; // SeriesLabels
args7[6] = true; // HasLegend
args7[7] = "Sample Chart"; // Title
args7[8] = "Sample Category Type"; // CategoryTitle
args7[9] = "Sample Value Type"; // ValueTitle
args7[10] = Missing.Value; // ExtraTitle
chart.GetType().InvokeMember("ChartWizard", BindingFlags.InvokeMethod, null, chart, args7);

Console.WriteLine ("Press ENTER to finish the sample:");
Console.ReadLine();

try {
// If user interacted with Excel it will not close when the app object is destroyed, so we close it explicitely
workbook.Saved = true;
app.UserControl = false;
app.Quit();
} catch (COMException) {
Console.WriteLine ("User closed Excel manually, so we don't have to do that");
}

Console.WriteLine ("Sample successfully finished!");
return 100;
}
}


110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧