将多个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(reportArrary[i]).Split('_')[1];//获取sheet名称

var excelStream = new FileStream(reportArrary[i], 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(reportArrary[i]);

}

}

}

}

}

}

合并新版本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("合并完成");

}

相关推荐
future_studio1 小时前
聊聊 Unity(小白专享、C# 小程序 之 加密存储)
开发语言·小程序·c#
c#上位机1 小时前
MefBootstrapper在Prism引导程序中的使用
c#·wpf·prism
玩泥巴的4 小时前
.NET驾驭Word之力:基于规则自动生成及排版Word文档
c#·word·.net·com互操作
SunnyDays10115 小时前
C# 实现高保真 Excel 转 PDF(无需 Office 环境)
经验分享·c#·excel转pdf
攻城狮CSU5 小时前
C# 数据加载专题 之泛型序列化
java·servlet·c#
爱编程的鱼5 小时前
C# 参数详解:从基础传参到高级应用
开发语言·microsoft·c#
流水线上的指令侠7 小时前
使用C#写微信小程序后端——电商微信小程序
微信小程序·小程序·c#·visual studio
gc_22998 小时前
C#编写的WebApi接口直接返回byte数组引发的问题
c#·byte数组
刘梦凡呀20 小时前
C#获取钉钉平台考勤记录
java·c#·钉钉
承渊政道20 小时前
动态内存管理
c语言·c++·经验分享·c#·visual studio