您的位置:首页 > 编程语言 > C#

C#操作excel

2008-04-06 20:57 381 查看
引用的com组件:Microsoft.Office.Interop.Excel.dll,一般安装完office就有这个文件。(office 2003下载这个dll:点击下载)。
新建一个C#项目,添加前面的dll到引用里面来
读取excel数据到dataset

方法一
private void OpenExcel(string strFileName)

public DataSet ExcelToDS(string Path)

dataset导出到excel

4.1 写数据

object missing = System.Reflection.Missing.Value;

Microsoft.Office.Interop.Excel.ApplicationClass appc = new Microsoft.Office.Interop.Excel.ApplicationClass();//lauch excel

Microsoft.Office.Interop.Excel.Workbook wb = appc.Application.Workbooks.Add(true);//add new workbook Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.get_Item(1);//get first worksheet

Microsoft.Office.Interop.Excel.Range range = ws.get_Range("A1", "C1");//get range from A1 to C1

range.Value2 = arrValue;//set value

public bool SaveFP2toExcel(string Path)

4.2 格式化

设置字体颜色
range.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);//white color
设置背景色
range.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.MidnightBlue);//set backgroud color
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: