C#读写Excel文件(二)

1、样式设置

string strDir = Directory.GetCurrentDirectory();

string fileName = strDir + @"\test.xls";

MSExcel.Application excelApp= new MSExcel.Application();

excelApp.Visible = true;//是打开可见

MSExcel.Workbooks wbks = excelApp.Workbooks;

MSExcel._Workbook wbk = wbks.Add(fileName);

 

object Nothing = Missing.Value;

MSExcel._Worksheet whs;// = wbk.Sheets.Add(Nothing, Nothing, Nothing, Nothing);

whs = wbk.Sheets[1];//获取第一张工作表

whs.Activate();

((MSExcel.Range)whs.Rows[1, Missing.Value]).Interior.ColorIndex = 3;//设置颜色

((MSExcel.Range)whs.Cells[1, "B"]).Font.Size = 8;//设置字体大小

((MSExcel.Range)whs.Rows[1, Missing.Value]).Font.Bold = true;//设置是否粗体

((MSExcel.Range)whs.Cells[2, 1]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//垂直居中

((MSExcel.Range)whs.Cells[2, 1]).HorizontalAlignment = XlHAlign.xlHAlignCenter;//水平居中

((MSExcel.Range)whs.Cells[3, 3]).Borders.LineStyle = 3;//边框

MSExcel.Range rang =whs.get_Range("A1", "B2");

rang.WrapText = true;//自动换行

rang.EntireColumn.AutoFit();//自动调整列宽

 

2、拷贝与粘贴

string strDir = Directory.GetCurrentDirectory();

string fileName = strDir + @"\test.xls";

MSExcel.Application excelApp= new MSExcel.Application();

excelApp.Visible = true;//是打开可见

MSExcel.Workbooks wbks = excelApp.Workbooks;

MSExcel._Workbook wbk = wbks.Add(fileName);

 

object Nothing = Missing.Value;

MSExcel._Worksheet whs;// = wbk.Sheets.Add(Nothing, Nothing, Nothing, Nothing);

whs = wbk.Sheets[1];//获取第一张工作表

whs.Activate();

MSExcel.Range rang = whs.get_Range("B1","B20");

rang.Select();//选中区域

rang.Copy();//拷贝到内存

rang = whs.Cells[2,1];//选定起始粘贴位置

rang.Select();

rang.Parse(Nothing,Nothing);//粘贴

 

3、向Excel写入数据

string strDir = Directory.GetCurrentDirectory();

string fileName = strDir + @"\test.xls";

MSExcel.Application excelApp= new MSExcel.Application();

excelApp.Visible = true;//是打开可见

MSExcel.Workbooks wbks = excelApp.Workbooks;

MSExcel._Workbook wbk = wbks.Add(fileName);

 

object Nothing = Missing.Value;

MSExcel._Worksheet whs;// = wbk.Sheets.Add(Nothing, Nothing, Nothing, Nothing);

whs = wbk.Sheets[3];//获取第一张工作表

whs.Activate();

 

DataTable dt = new DataTable();

dt.Columns.Add("姓名");

dt.Columns.Add("年龄");

dt.Columns.Add("性别");

dt.Rows.Add("姓名","年龄","性别");

dt.Rows.Add("张三","23","男");

dt.Rows.Add("李思", "12", "女");

dt.Rows.Add("张琴", "33", "女");

dt.Rows.Add("王高", "62", "男");

dt.Rows.Add("郑涛", "56", "男");

int rowIndex=1;

foreach (DataRow row in dt.Rows)

{

for (int colIndex = 0; colIndex < dt.Columns.Count; colIndex++)

{

whs.Cells[rowIndex, colIndex+1] = row[colIndex].ToString();

}

rowIndex++;

}

wbk.Close();//关闭文档

wbks.Close();//关闭工作簿

excelApp.Quit();//关闭excel应用程序

System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);//释放excel进程

excelApp = null;

 

4、使用OLEDB方式从EXCEL读取数据

【例一】从Excel文件test.xls中的工作表StudentAttendence中读取数据并显示在DataGridView控件dataGridView1上。

使用名字空间

using System.Data.OleDb;

using System.IO;

 

string strDir = Directory.GetCurrentDirectory();

string fileName = strDir + @"\test.xls";

string strCon = "Provider = Microsoft.Jet.OleDb.4.0; Data Source =" + fileName + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";

OleDbConnection conn = new OleDbConnection(strCon);

string sql = "select * from [StudentAttendence]";

conn.Open();

OleDbDataAdapter myCommand = new OleDbDataAdapter(sql, strCon);

DataSet ds = new DataSet();

myCommand.Fill(ds, "[StudentAttendence]");

conn.Close();

dataGridView1.DataMember = "[StudentAttendence]";

dataGridView1.DataSource = ds;

你可能感兴趣的:(C#)