标题:[求助]C#操作Excel的问题
只看楼主
小海龟
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1068
专家分:4
注 册:2006-8-1
 问题点数:0 回复次数:7 
[求助]C#操作Excel的问题
我现在在做一个数据导入导出的东东,我用的是.net2005,在windows form里面操作Excel文件,哪位知道告诉我好么,不胜感激!
搜索更多相关主题的帖子: Excel 
2006-08-09 09:32
炫舞鱼
Rank: 1
等 级:新手上路
帖 子:85
专家分:0
注 册:2006-5-16
得分:0 
真不好意思~
我是2003的
如果需要我再发给你吧
你是要导入还是导出啊
2006-08-09 16:03
小海龟
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1068
专家分:4
注 册:2006-8-1
得分:0 

导出到Excel文件的。


[bc09] 犯强汉者,虽远比诛!
2006-08-09 16:05
chenjin145
Rank: 1
等 级:禁止访问
帖 子:3922
专家分:0
注 册:2006-7-12
得分:0 
[CODE]

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;
}
}

[/CODE]

直接msdn複製過來的

[url=javascript:alert(1);] [div]fdgfdgfdg\" on\"[/div] [/url]
2006-08-09 16:22
小海龟
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1068
专家分:4
注 册:2006-8-1
得分:0 

我试试看,先谢谢了。


[bc09] 犯强汉者,虽远比诛!
2006-08-09 16:32
小海龟
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1068
专家分:4
注 册:2006-8-1
得分:0 
帅呆了,可以用。但是我要的是直接保存Excel文件,而不需手动去保存文件的。哪位有回帖或发给我好不?感激不尽!99service@163.com

[bc09] 犯强汉者,虽远比诛!
2006-08-09 16:43
volte
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:69
帖 子:1167
专家分:1316
注 册:2004-12-19
得分:0 

异常的时候好像没有销毁对象.!
存在内存的占用问题.


大家都是朋友,有空就来坐坐!
2006-08-09 16:49
小海龟
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1068
专家分:4
注 册:2006-8-1
得分:0 

恩,加个GC.Collect();就好了。


[bc09] 犯强汉者,虽远比诛!
2006-08-09 16:50



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-82926-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.148466 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved