这里有详细讲解优缺点
1.使用Office自带的库
需要用户机安装office
Microsoft.Office.Interop.Excel
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
private void btn_Office_Click(object sender, EventArgs e)
{
string importExcelPath = "E:\\import.xlsx";
string exportExcelPath = "E:\\export.xlsx";
//创建
Excel.Application xlApp = new Excel.Application();
xlApp.DisplayAlerts = false;
xlApp.Visible = false;
xlApp.ScreenUpdating = false;
//打开Excel
Excel.Workbook xlsWorkBook = xlApp.Workbooks.Open(importExcelPath, System.Type.Missing, System.Type.Missing, System.Type.Missing,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
//处理数据过程,更多操作方法自行百度
Excel.Worksheet sheet = xlsWorkBook.Worksheets[1];//工作薄从1开始,不是0
sheet.Cells[1, 1] = "test";
//另存
xlsWorkBook.SaveAs(exportExcelPath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
//关闭Excel进程
ClosePro(xlApp, xlsWorkBook);
}
public void ClosePro(Excel.Application xlApp, Excel.Workbook xlsWorkBook)
{
if (xlsWorkBook != null)
xlsWorkBook.Close(true, Type.Missing, Type.Missing);
xlApp.Quit();
// 安全回收进程
System.GC.GetGeneration(xlApp);
IntPtr t = new IntPtr(xlApp.Hwnd); //获取句柄
int k = 0;
GetWindowThreadProcessId(t, out k); //获取进程唯一标志
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
p.Kill(); //关闭进程
}
2.使用NPOI
xlsx格式导出,可能有问题
using System.IO;
using NPOI;
using NPOI.SS.UserModel;
private void btn_NPOI_Click(object sender, EventArgs e)
{
string importExcelPath = "E:\\import.xlsx";
string exportExcelPath = "E:\\export.xlsx";
IWorkbook workbook = WorkbookFactory.Create(importExcelPath);
ISheet sheet = workbook.GetSheetAt(0);//获取第一个工作薄
IRow row = (IRow)sheet.GetRow(0);//获取第一行
//设置第一行第一列值,更多方法请参考源官方Demo
row.CreateCell(0).SetCellValue("test");//设置第一行第一列值
//导出excel
FileStream fs = new FileStream(exportExcelPath, FileMode.Create, FileAccess.ReadWrite);
workbook.Write(fs);
fs.Close();
}
3.使用ClosedXml
从Nuget下载 ClosedXml
using ClosedXML;
using ClosedXML.Excel;
private void btn_ClosedXML_Click(object sender, EventArgs e)
{
string importExcelPath = "E:\\import.xlsx";
string exportExcelPath = "E:\\export.xlsx";
var workbook = new XLWorkbook(importExcelPath);
IXLWorksheet sheet = workbook.Worksheet(1);//这个库也是从1开始
//设置第一行第一列值,更多方法请参考官方Demo
sheet.Cell(1, 1).Value = "test";//该方法也是从1开始,非0
workbook.SaveAs(exportExcelPath);
}
4.使用 spire.xls
spire分免费和收费,无特殊需求用免费即可
using Spire.Xls;
private void btnSpire_Click(object sender, EventArgs e)
{
string importExcelPath = "E:\\import.xlsx";
string exportExcelPath = "E:\\export.xlsx";
Spire.Xls.Workbook workbook = new Spire.Xls.Workbook();
workbook.LoadFromFile(importExcelPath);
//处理Excel数据,更多请参考官方Demo
Spire.Xls.Worksheet sheet = workbook.Worksheets[0];
sheet.Range[1,1].Text = "test";//该方法也是从1开始,非0
workbook.SaveToFile(exportExcelPath);
}
使用OLEDB
用OLEDB方式读取EXCEL的速度是非常快的。但是当Excel数据量很大时。会非常占用内存,当内存不够时会抛出内存溢出的异常。
OLEDB方式将Excel作为一个数据源,直接用Sql语句操作数据,并且不需要安装Office Excel就可以使用。但缺点是不能灵活操作Excel,例如设置字体,单元格格式等。
参考 https://blog.csdn.net/qq_33459369/article/details/79308361