将多个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("合并完成");

}

相关推荐
zxy284722530110 小时前
C# 音频倍速播放
c#·音频·naudio·soundtouch·倍速
Water_Sunzhipeng13 小时前
HandyControl Demo 设置特定框架记录
c#
czhc114007566314 小时前
插补、另存为、标记过滤、采集超时——今天聊的四件事
c#
小羊没烦恼!15 小时前
Memory 记忆设计讨论:Agent Memory 的数据模型可以怎么设计
java·开发语言·前端·c++·c#
小羊没烦恼!18 小时前
Memory 记忆设计讨论:为什么 Agent Memory 不能只靠向量数据库?
java·开发语言·windows·算法·c#
...62219 小时前
C# 中 Lambda 表达式的全面讲解
c#
...62220 小时前
WinForm中Socket通信服务端客户端代码实现讲解
tcp/ip·c#·socket
唐青枫2 天前
别只会看日志:C#.NET SOS 调试扩展从 Dump 到内存泄漏排查
c#·.net
yswenli2 天前
基于C#开发网站视频嗅探下载工具——BrowserVideoGrabber
c#·videodownload·webvideodownload·videograbber
Lost of 程序猿2 天前
M2 实战:询价与采购订单聚合落地——跨聚合一致性、领域事件与 Outbox
开发语言·c#·asp.net·.net