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

}

相关推荐
yivifu13 小时前
精益求精,支持处理嵌套表格的Word表格转HTML表格
开发语言·c#·word
唐青枫14 小时前
C#.NET 全局异常到底怎么做?最完整的实战指南
c#·.net
Charles_go1 天前
C#13、什么是部分类
开发语言·c#
ghie90901 天前
C#语言中使用“using“关键字的介绍
开发语言·c#
csdn_wuwt1 天前
有C#可用的开源的地图吗?
后端·c#·gis·map·开发·设计·地图
6极地诈唬1 天前
【C#-sqlSugar-sqlite】在Windows从源码编译构建System.Data.SQLite.dll的方法
windows·sqlite·c#
我只有一台windows电脑1 天前
C# 对多个任务进行符合管理
c#
数据的世界011 天前
JAVA和C#的语法对比
java·windows·c#
csdn_aspnet2 天前
WPF 做一个简单的电子签名板(一)
c#·wpf
玖笙&2 天前
✨WPF编程进阶【7.2】:动画类型(附源码)
c++·c#·wpf·visual studio