请教:C#中如何对excel操作?

lanyuanlin 2003-07-21 08:45:24
如何将Excel的对象库文件转换成为dll?
...全文
66 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
TT008 2003-12-15
  • 打赏
  • 举报
回复
study
dahuzizyd 2003-07-21
  • 打赏
  • 举报
回复
这里:
http://xml.sz.luohuedu.net/xml/ShowDetail.asp?id=4EB79F05-B9A4-4E8A-836F-864393F40405
Xanadu00 2003-07-21
  • 打赏
  • 举报
回复

Excel.Application excelapp=new Excel.ApplicationClass();
Excel.Workbook wkbook=excelapp.Workbooks.Add(true);
Excel.Worksheet wksheet=(Excel.Worksheet)wkbook.ActiveSheet;
Excel.Range ra;

excelapp.Visible=True;

ra=wksheet.get_Range("a3","b5");
ra.Value="测试";
ra.Font.Name="宋体";
ra.Font.Size=24;
ra.Font.ColorIndex=5;
ra.HorizontalAlignment=Excel.XlHAlign.xlHAlignCenter;//水平方向居中
ra.Cells.Borders.LineStyle=Excel.XlLineStyle.xlContinuous;//加表格线
ra.Borders.Weight=Excel.XlBorderWeight.xlThin;//细线

//外边框加粗
ra.Borders[Excel.XlBordersIndex.xlEdgeLeft].Weight=Excel.XlBorderWeight.xlMedium;
ra.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight=Excel.XlBorderWeight.xlMedium;
ra.Borders[Excel.XlBordersIndex.xlEdgeTop].Weight=Excel.XlBorderWeight.xlMedium;
ra.Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight= Excel.XlBorderWeight.xlMedium;
ra.EntireColumn.AutoFit();//自动调整列宽
//ra.WrapText=true;//自动换行
ra.VerticalAlignment=Excel.XlVAlign.xlVAlignCenter;//竖直方向居中
ra.EntireRow.AutoFit();//自动调整行高

CMIC 2003-07-21
  • 打赏
  • 举报
回复
C#如何在Form中嵌入并且操作Excel表格
http://www.csdn.net/Develop/Read_Article.asp?Id=18344
ArEoN 2003-07-21
  • 打赏
  • 举报
回复
再给个例子

using System;
using System.Reflection;
using System.Runtime.InteropServices;
using Excel;

class Excel {
public static int Main() {
Application exc = new Application();
if (exc == null) {
Console.WriteLine("ERROR: EXCEL couldn"t be started!");
return 0;
}

exc.set_Visible(0, true);
Workbooks workbooks = exc.Workbooks;
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet, 0);
Sheets sheets = workbook.Worksheets;

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

Range range1 = worksheet.get_Range("C1", Missing.Value);
if (range1 == null) {
Console.WriteLine ("ERROR: range == null");
}
const int nCells = 1;
Object[] args1 = new Object[1];
args1[0] = nCells;
range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, null,range1, args1);
return 100;
}
}
//使用数组
const int nCell = 5;
Range range2 = worksheet.get_Range("A1", "E1");
int[] array2 = new int [nCell];
for (int i=0; i < array2.GetLength(0); i++) {
array2[i] = i+1;
}
Object[] args2 = new Object[1];
args2[0] = array2;
range2.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, range2,args2);
ArEoN 2003-07-21
  • 打赏
  • 举报
回复
//创建一个新Excel的Application
Application exc = new Application();
if (exc == null) {
Console.WriteLine("ERROR: EXCEL couldn"t be started");
return 0;
}
//让这个工程可见
exc.set_Visible(0, true);
//获取WorkBooks集合
Workbooks workbooks = exc.Workbooks;
//加入新的WorkBook
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet, 0);
//获取WorkSheets集合
_Worksheet worksheet = (_Worksheet) sheets.get_Item(1);
if (worksheet == null) {
Console.WriteLine ("ERROR in worksheet == null");
}
// 给单元格设置变量
Range range1 = worksheet.get_Range("C1", Missing.Value);
if (range1 == null) {
Console.WriteLine ("ERROR: range == null");
}
const int nCells = 1;
Object[] args1 = new Object[1];
args1[0] = nCells;
range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, range1, args1);
panyee 2003-07-21
  • 打赏
  • 举报
回复
tlbimp excel.exe /out:excel.dll
chinchy 2003-07-21
  • 打赏
  • 举报
回复
http://www.microsoft.com/china/msdn/library/dnexcl2k2/html/odc_offcs.asp

110,533

社区成员

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

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

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