注册 登录
两全其美网校城 返回首页

carefulbe的个人空间 http://www.lqqm.com/?10180 [收藏] [复制] [分享] [RSS]

日志

[转帖]C#读写EXCEL

已有 919 次阅读2009-3-18 14:03 |个人分类:编程相关|

 
2008-12-19 16:22

C#读写EXCEL方法一
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Reflection;
using System.IO;


namespace ExcelTest
{
     class Program
     {
         static void Main(string[] args)
         {
             ////创建Application对象
             Excel.Application xlsApp = new Excel.Application();
             if (xlsApp == null)
             {
                 return;
             }

             xlsApp.Visible = true;

          
             //得到WorkBook对象, 可以用两种方式
             //之一: 打开已有的文件
             //Excel.Workbook xlsBook = xlsApp.Workbooks.Open(@"E:\Documents and Settings\daniel.chen\Desktop\test.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.Workbook xlsBook = xlsApp.Workbooks.Add(Missing.Value);


             //指定要操作的Sheet,两种方式
             //之一:
             Excel.Worksheet xlsSheet = (Excel.Worksheet)xlsBook.Sheets[1];
             //之二:
             //Excel.Worksheet xlsSheet = (Excel.Worksheet)xlsApp.ActiveSheet;


             //指定单元格,读取数据,两种方法
             //之一:
             Excel.Range range1 = xlsSheet.get_Range("C2",Type.Missing);
             Console.WriteLine(range1.Value2);
             //之二:
             Excel.Range range2 = (Excel.Range)xlsSheet.Cells[2, 3];
             Console.WriteLine(range2.Value2);


             //在单元格中写入数据
             Excel.Range range3 = xlsSheet.get_Range("A1",Type.Missing);
             range3.Value2 = "Hello World!";
             range3.Borders.Color = Color.FromArgb(123, 231, 32).ToArgb();
             range3.Font.Color = Color.Red.ToArgb();
             range3.Font.Name = "Arial";
             range3.Font.Size = 9;
             //range3.Orientation = 90;   //vertical
             range3.Columns.HorizontalAlignment = Excel.Constants.xlCenter;
             range3.VerticalAlignment = Excel.Constants.xlCenter;
             range3.Interior.Color = Color.FromArgb(192,192,192).ToArgb();
             range3.Columns.AutoFit();//adjust the column width automatically


             //在某个区域写入数据数组
             int matrixHeight = 20;
             int matrixWidth = 20;
             string[,] martix=new string[matrixHeight,matrixWidth];
             for (int i = 0; i < matrixHeight; i++)
                 for (int j = 0; j < matrixWidth; j++)
                 {
                     martix[i, j] = String.Format("{0}_{1}", i+1, j+1);
                 }
             string startColName=GetColumnNameByIndex(0);
             string endColName=GetColumnNameByIndex(matrixWidth-1);
             //取得某个区域,两种方法
             //之一:
             Excel.Range range4 = xlsSheet.get_Range("A1",Type.Missing);
             range4 = range4.get_Resize(matrixHeight,matrixWidth);
             //之二:
             //Excel.Range range4 = xlsSheet.get_Range(String.Format("{0}{1}", startColName, 1), String.Format("{0}{1}", endColName, martixHeight));
             range4.Value2 = martix;
             range4.Font.Color = Color.Red.ToArgb();
             range4.Font.Name="Arial";
             range4.Font.Size = 9;
             range4.Columns.HorizontalAlignment = Excel.Constants.xlCenter;


             //设置column和row的宽度和颜色
             int columnIndex=3;
             int rowIndex=3;
             string colName = GetColumnNameByIndex(columnIndex);
             xlsSheet.get_Range(colName + rowIndex.ToString(), Type.Missing).Columns.ColumnWidth = 20;
             xlsSheet.get_Range(colName + rowIndex.ToString(), Type.Missing).Rows.RowHeight = 40;
             xlsSheet.get_Range(colName + rowIndex.ToString(), Type.Missing).Columns.Interior.Color = Color.Blue.ToArgb();//单格颜色
             xlsSheet.get_Range(5 + ":" + 7, Type.Missing).Rows.Interior.Color = Color.Yellow.ToArgb();//第5行到第7行的颜色
             //xlsSheet.get_Range("G : G", Type.Missing).Columns.Interior.Color=Color.Pink.ToArgb();//第n列的颜色如何设置??

             //保存,关闭
             if (File.Exists(@"E:\Documents and Settings\daniel.chen\Desktop\test1.xls"))
             {
                 File.Delete(@"E:\Documents and Settings\daniel.chen\Desktop\test1.xls");
             }
             xlsBook.SaveAs(@"E:\Documents and Settings\daniel.chen\Desktop\test1.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
             xlsBook.Close(false, Type.Missing, Type.Missing);
             xlsApp.Quit();

             GC.Collect();

             Console.ReadKey();
         }

         //将column index转化为字母,至多两位
         public static string GetColumnNameByIndex(int index)
         {
             string[] alphabet = new string[] {"","A", "B", "C", "D", "E", "F", "G", "H", "I", "J" ,"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
             string result = "";
             int temp = index / 26;
             int temp2 = index % 26 + 1;
             if (temp > 0)
             {
                 result += alphabet[temp];
             }
             result += alphabet[temp2];
             return result;
         }
     }
}

C#读写EXCEL方法二
通过数据库方式

   OleDbDataAdapter    ada    =    new    OleDbDataAdapter("SELECT    *    FROM    [Sheet1$]",    "Provider=Microsoft.Jet.OLEDB.4.0;Data    Source="    +    openFileDialog1.FileName    +    ";Extended    Properties=Excel    8.0;");  
   DataTable    dt    =    new    DataTable();  
   try  
   {  
   ada.Fill(dt);  
   }  
   catch(Exception    ex)  
   {  
   MessageBox.Show(ex.ToString());  
   }  
   dataGrid1.DataSource    =    dt; 

1

鸡蛋

鲜花

握手

雷人

路过

发表评论 评论 (2 个评论)

回复 hardyears 2009-3-18 17:08
高人呀!我正面临一个c#的编程问题,你帮我解决一下吧
回复 carefulbe 2009-3-30 17:05
相互学习吧!:)

 

 

 

Baidu
中华会计网校 新东方网络课堂 中华会计网校会计继续教育 新东方网校 环球网校 中公网校

小黑屋|手机版|关于我们|两全其美网校城 ( 京ICP备05068258-34 )

GMT+8, 2024-5-7 10:21

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部