首先,本文的write和read都可以成功运行。(环境:VS2012,office2016)
不过需要添加一个excel引用。并且在代码的开头需要调用该引用的命名空间,使用别名。using Excel = Microsoft.Office.Interop.Excel;
写入和读取代码和其他程序一样都有很多种方法,本文只是各展示了一种方法,因为本人时间和精力有限。对于写入代码可能需要使用循环写入,对于读取代码是一次性读取的。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel; //需要手动添加引用
namespace ReadExcel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string filepath = "C:\\Users\\Administrator\\Desktop\\LaserFitting\\result.xlsx";
OpenExcel(filepath);
}
//将数据写入已存在Excel
public static void writeExcel(double[] result, string filepath)
{
//1.创建Applicaton对象
Microsoft.Office.Interop.Excel.Application xApp = new
Microsoft.Office.Interop.Excel.Application();
//2.得到workbook对象,打开已有的文件
Microsoft.Office.Interop.Excel.Workbook xBook = xApp.Workbooks.Open(filepath,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value);
//3.指定要操作的Sheet
Microsoft.Office.Interop.Excel.Worksheet xSheet = (Microsoft.Office.Interop.Excel.Worksheet)xBook.Sheets[1];
//在第一列的左边插入一列 1:第一列
//xlShiftToRight:向右移动单元格 xlShiftDown:向下移动单元格
//Range Columns = (Range)xSheet.Columns[1, System.Type.Missing];
//Columns.Insert(XlInsertShiftDirection.xlShiftToRight, Type.Missing);
//4.向相应对位置写入相应的数据
xSheet.Cells[1][1] = result[1];
//5.保存保存WorkBook
xBook.Save();
//6.从内存中关闭Excel对象
xSheet = null;
xBook.Close();
xBook = null;
//关闭EXCEL的提示框
xApp.DisplayAlerts = false;
//Excel从内存中退出
xApp.Quit();
xApp = null;
}
private void OpenExcel(string strFileName)
{
object missing = System.Reflection.Missing.Value;
Excel.Application excel = new Excel.Application();//lauch excel application
if (excel == null)
{
this.label1.Text = "Can't access excel";
}
else
{
excel.Visible = false; excel.UserControl = true;
// 以只读的形式打开EXCEL文件
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; //得到行数
//int columnsint = mySheet.UsedRange.Cells.Columns.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; //get range's value
object[,] arry2 = (object[,])rng2.Value2;
object[,] arry3 = (object[,])rng3.Value2; //get range's value
object[,] arry4 = (object[,])rng4.Value2;
//将新值赋给一个数组
string[,] arry = new string[rowsint - 1, 4];
//for (int i = 1; i <= rowsint - 1; i++)
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();
}
}
}
参考写入链接1
参考读取链接2