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
新建一个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
相关文章推荐
- C#操作Excel:不存在类型或命名空间名称“Interop”(
- C#操作Excel总结
- VS2005[C#] 操作 Excel 全攻略
- C#对Excel的一些操作【一】
- c# 操作excel生成pdf
- c# 操作Excel
- C#合并excel单元格,操作合并后的单元格 (winform导出excel)
- C#操作Excel数据导入和dataGridView重绘
- C# 操作Excel图形——绘制、读取、隐藏、删除图形
- C#操作Excel---技巧篇(上)
- MyXls组件操作Excel报表(C#)
- c#操作excel方式二:采用OleDB操作Excel文件
- C#操作Excel文件(读取Excel,写入Excel)
- C#操作Excel
- c#操作wps中的excel
- C# 操作 Excel 文件(.xls 或 .xlsx)
- C#对Excel的样式操作
- C#操作excel(多种方法比较)
- C#操作Excel总结
- C#与excel互操作的错误无法将类型为“Excel.ApplicationClass”的COM 对象强制转换为接口类型“Excel._Application”