1、引用Microsoft.Office.Interop.Excel.dll
2、引用命名空间、使用别名
- using System.Reflection;
-
- using Excel = Microsoft.Office.Interop.Excel;
3.写入excel
写入函数
- public void ToExcel(string strTitle)
- {
- int nMax = 9;
- int nMin = 4;
-
- int rowCount = nMax - nMin + 1;
-
- const int columnCount = 4;
-
-
-
-
-
- Excel.Application excelApp = new Excel.ApplicationClass();
-
-
-
-
-
- Excel.Workbook workBook = excelApp.Workbooks.Add(true);
-
-
-
-
-
- Excel.Worksheet worksheet = workBook.ActiveSheet as Excel.Worksheet;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- string[] strHead = new string[columnCount] { "序号", "范围", "分组1", "分组2" };
-
- int[] columnWidth = new int[4] { 8, 16, 8, 10 };
-
- for (int i = 0; i < columnCount; i++)
- {
-
-
-
- Excel.Range headRange = worksheet.Cells[1, i + 1] as Excel.Range;
-
- headRange.Value2 = strHead[i];
-
- headRange.Font.Name = "宋体";
-
- headRange.Font.Size = 12;
-
- headRange.Font.Bold = false;
-
- headRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
-
- headRange.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
-
- headRange.ColumnWidth = columnWidth[i];
-
-
-
-
-
- }
-
-
-
-
-
- for (int i = 0; i < columnCount; i++)
- {
-
-
-
- Excel.Range contentRange = worksheet.get_Range(worksheet.Cells[2, i + 1], worksheet.Cells[rowCount - 1 + 3, i + 1]);
-
- contentRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
-
- contentRange.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
-
-
-
-
-
- contentRange.WrapText = true;
-
- contentRange.NumberFormatLocal = "@";
-
- }
-
-
-
-
-
- for (int i = nMin; i <= nMax; i++)
- {
-
- int k = i - nMin;
-
-
-
-
-
-
-
-
-
- excelApp.Cells[k + 2, 1] = string.Format("{0}", k + 1);
-
- excelApp.Cells[k + 2, 2] = string.Format("{0}-{1}", i - 0.5, i + 0.5);
-
- excelApp.Cells[k + 2, 3] = string.Format("{0}", k + 3);
-
- excelApp.Cells[k + 2, 4] = string.Format("{0}", k + 4);
-
- }
-
-
-
-
-
- excelApp.Visible = true;
-
- }
写入按钮函数:
- private void button1_Click(object sender, EventArgs e)
- {
- ToExcel("方式3");
-
- }
结果:

4.读取excel
添加一个opendiog用于选择要读取的excel
命名空间
- using System.Diagnostics;
读取函数:
- private void OpenExcel(string strFileName)
- {
- object missing = System.Reflection.Missing.Value;
- Excel.Application excel = new Excel.ApplicationClass();
- if (excel == null)
- {
- this.label1.Text="Can‘t access excel";
- }
- else
- {
- excel.Visible = false; excel.UserControl = true;
-
- Excel.Workbook wb = excel.Application.Workbooks.Open(strFileName, missing, true, missing, missing, missing,
- missing, missing, missing, true, missing, missing, missing, missing, missing);
-
- Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets.get_Item(1);
-
- int rowsint = ws.UsedRange.Cells.Rows.Count;
-
-
- Excel.Range rng1 = ws.Cells.get_Range("A2", "A" + rowsint);
- Excel.Range rng2 = ws.Cells.get_Range("B2", "B" + rowsint);
- Excel.Range rng3 = ws.Cells.get_Range("C2", "C" + rowsint);
- Excel.Range rng4 = ws.Cells.get_Range("D2", "D" + rowsint);
- object[,] arry1 = (object[,])rng1.Value2;
- object[,] arry2 = (object[,])rng2.Value2;
- object[,] arry3 = (object[,])rng3.Value2;
- object[,] arry4 = (object[,])rng4.Value2;
-
- string[,] arry = new string[rowsint - 1, 4];
-
- for (int i = 1; i <= rowsint - 2; i++)
- {
-
- arry[i - 1, 0] = arry1[i, 1].ToString();
-
- arry[i - 1, 1] = arry2[i, 1].ToString();
-
- arry[i - 1, 2] = arry3[i, 1].ToString();
-
- arry[i - 1, 3] = arry4[i, 1].ToString();
- }
- string a = "";
- for (int i = 0; i <= rowsint - 3; i++)
- {
- a += arry[i, 0] + "|" + arry[i, 1] + "|" + arry[i, 2] + "|" + arry[i, 3]+"\n";
-
- }
- this.label1.Text=a;
- }
- excel.Quit(); excel = null;
- Process[] procs = Process.GetProcessesByName("excel");
- foreach (Process pro in procs)
- {
- pro.Kill();
- }
- GC.Collect();
- }
读取按钮代码:
- private void button1_Click(object sender, EventArgs e)
- {
- if (openFileDialog.ShowDialog() == DialogResult.OK)
- {
- OpenExcel(openFileDialog.FileName);
- }
-
- }
结果;

(转自http://blog.csdn.net/zzq900503/article/details/8808659)
c#操作excel方式三:使用Microsoft.Office.Interop.Excel.dll读取Excel文件,布布扣,bubuko.com
c#操作excel方式三:使用Microsoft.Office.Interop.Excel.dll读取Excel文件
原文:http://www.cnblogs.com/zhaoxinshanwei/p/3590427.html