将多个EXCEL 合并一个EXCEL多个sheet

合并老版本xls

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using NPOI.HSSF.UserModel;

using System.IO;

using NPOI.XSSF.UserModel;

namespace Merge_Excel

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

bool Result;

string\[\] FileNames;

FileNames = GetFileNames();

if (FileNames == null) { return; }

var fullWork = new HSSFWorkbook();

// 遍历读取每一个订单文件

foreach (string FileName in FileNames)

{

string sheetName = Path.GetFileNameWithoutExtension(FileName);//获取sheet名称

var excelStream = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

var workbook = new HSSFWorkbook(excelStream);//读取excel文件

HSSFSheet sheet = workbook.GetSheetAt(0) as HSSFSheet; //获取第一个工作表(sheet)

sheet.CopyTo(fullWork, sheetName, true, true);//将报表合并至综合报表中

excelStream.Close();

}

using (FileStream filestream = new FileStream(Application.StartupPath + "合并" + System.DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls", FileMode.Create))

{

fullWork.Write(filestream);

filestream.Close();

}

fullWork.Dispose();

fullWork.Close();

fullWork = null;

GC.Collect();

MessageBox.Show("合并完成");

}

public string\[\] GetFileNames()

{

OpenFileDialog ofd = new OpenFileDialog();

ofd.Filter = "所有文件|*.*|office2007及高版本|*.xlsx|office2003及老版本|*.xls";

ofd.Multiselect = true;

if (ofd.ShowDialog() == DialogResult.OK)

{

if (ofd.FileNames != null)

{

return ofd.FileNames;

}

}

return null;

}

private void openFileDialog1_FileOk(object sender, CancelEventArgs e)

{

var fullWork = new HSSFWorkbook();//最终的合并excel

//string exportReports = hidExportReports.Value;//需要合并的Excel文件路径

string exportReports = "";

if (!string.IsNullOrEmpty(exportReports))

{

var reportArrary = exportReports.Split(',');

try

{

for (int i = 0; i < reportArrary.Length; i++)

{

string sheetName = Path.GetFileNameWithoutExtension(reportArraryi).Split('_')1;//获取sheet名称

var excelStream = new FileStream(reportArraryi, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

var workbook = new HSSFWorkbook(excelStream);//读取excel文件

HSSFSheet sheet = workbook.GetSheetAt(0) as HSSFSheet; //获取第一个工作表(sheet)

sheet.CopyTo(fullWork, sheetName, true, true);//将报表合并至综合报表中

}

var stream = new MemoryStream();

fullWork.Write(stream);

byte\[\] bytes = stream.ToArray();

// Now that you have all the bytes representing the PDF report, buffer it and send it to the client.

//Response.Buffer = true;

//Response.Clear();

//Response.ContentType = "application/vnd.ms-excel";

//String filename = HttpUtility.UrlEncode("综合报表", Encoding.UTF8);

//Response.AddHeader("content-disposition", "attachment; filename=" + filename + ".xls");

//Response.BinaryWrite(bytes); // create the file

//Response.Flush(); // send it to the client to download

using (FileStream filestream = new FileStream(System.IO.Path.Combine(@"D:\", "liumang.xlsx"), FileMode.Create)) { fullWork.Write(filestream); filestream.Close(); }

MessageBox.Show("导出完成");

}

catch

{

}

finally

{

//删除excel文件

for (int i = 0; i < reportArrary.Length; i++)

{

File.Delete(reportArraryi);

}

}

}

}

}

}

合并新版本xlsx

private void button1_Click(object sender, EventArgs e)

{

bool Result;

string\[\] FileNames;

FileNames = GetFileNames();

if (FileNames == null) { return; }

var fullWork = new XSSFWorkbook();

// 遍历读取每一个订单文件

foreach (string FileName in FileNames)

{

string sheetName = Path.GetFileNameWithoutExtension(FileName);//获取sheet名称

var excelStream = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

var workbook = new XSSFWorkbook(excelStream);//读取excel文件

XSSFSheet sheet = workbook.GetSheetAt(0) as XSSFSheet; //获取第一个工作表(sheet)

sheet.CopyTo(fullWork, sheetName, true, true);//将报表合并至综合报表中

excelStream.Close();

}

using (FileStream filestream = new FileStream(Application.StartupPath + "合并" + System.DateTime.Now.ToString("yyyyMMddhhmmss") + ".xlsx", FileMode.Create))

{

fullWork.Write(filestream);

filestream.Close();

}

fullWork.Dispose();

fullWork.Close();

fullWork = null;

GC.Collect();

MessageBox.Show("合并完成");

}

相关推荐
Behavior1 小时前
国内下载使用 Unity 6000 踩坑点总结
c#·unity3d·游戏开发
czhc11400756632 小时前
一张图从哪来,一个判断往哪走
c#
曹牧13 小时前
C#:未被引用的函数
c#
上位机妹子18 小时前
EF Core 两种继承映射:每层次一张表(TPH)与每类型一张表(TPT)
数据库·c#·.netcore·ef core
小羊没烦恼!20 小时前
jQuery1.5的改进细节
java·服务器·开发语言·前端·c#
下页、再停留21 小时前
【C#桌面客户端系列学习-8】批量导出数据为csv格式
开发语言·c#·visual studio
波力海苔夹心脆6751 天前
C# 视觉检测实战:PLC 按钮触发 VisionPro 检测,OK 亮灯 / NG 灭灯(西门子 PLC,含界面显示与图片存档)
开发语言·经验分享·c#·自动化·视觉检测·.net
kim1 天前
一个人 + AI,用 WinForms 搓了个 376KB 的 Windows 便签:便签 / 截图 / 标注 / OCR / 待办全塞进去了
c#
hez20101 天前
一行代码在 .NET 项目里启用 Satori GC
c#·.net·.net core·gc·clr
宇卿.1 天前
C#语句:
开发语言·c#