NPOI,将多个excel合并为1个
想要把多个excel合并为1个excel,发现NPOI的HSSFSheet里面有个CopyTo的方法,(publicvoidCopyTo(HSSFWorkbookdest...
想要把多个excel合并为1个excel,发现NPOI的HSSFSheet里面有个CopyTo的方法,( public void CopyTo(HSSFWorkbook dest, string name, bool copyStyle, bool keepFormulas);)
官方代码:
using System;
using System.Windows.Forms;
using NPOI.HSSF.UserModel;
using System.IO;
namespace CopySheet
{
class Program
{
[STAThread]
static void Main(string[] args)
{
//Excel worksheet combine example
//You will be prompted to select two Excel files. test.xls will be created that combines the sheets
//Note: This example does not check for duplicate sheet names. Your test files should have different sheet names.
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Excel document (*.xls)|*.xls";
ofd.Title = "Select first Excel document";
if (ofd.ShowDialog() == DialogResult.OK)
{
HSSFWorkbook book1 = new HSSFWorkbook(new FileStream(ofd.FileName, FileMode.Open));
ofd.Title = "Select second Excel document";
if (ofd.ShowDialog() == DialogResult.OK)
{
HSSFWorkbook book2 = new HSSFWorkbook(new FileStream(ofd.FileName, FileMode.Open));
HSSFWorkbook product = new HSSFWorkbook();
for (int i = 0; i < book1.NumberOfSheets; i++)
{
HSSFSheet sheet1 = book1.GetSheetAt(i) as HSSFSheet;
sheet1.CopyTo(product, sheet1.SheetName, true, true);
}
for (int j = 0; j < book2.NumberOfSheets; j++)
{
HSSFSheet sheet2 = book2.GetSheetAt(j) as HSSFSheet;
sheet2.CopyTo(product, sheet2.SheetName, true, true);
}
product.Write(new FileStream("test.xls", FileMode.Create, FileAccess.ReadWrite));
}
}
}
}
}
我运行官方代码,结果也是报错,大家有没有好的解决方法呢 展开
官方代码:
using System;
using System.Windows.Forms;
using NPOI.HSSF.UserModel;
using System.IO;
namespace CopySheet
{
class Program
{
[STAThread]
static void Main(string[] args)
{
//Excel worksheet combine example
//You will be prompted to select two Excel files. test.xls will be created that combines the sheets
//Note: This example does not check for duplicate sheet names. Your test files should have different sheet names.
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Excel document (*.xls)|*.xls";
ofd.Title = "Select first Excel document";
if (ofd.ShowDialog() == DialogResult.OK)
{
HSSFWorkbook book1 = new HSSFWorkbook(new FileStream(ofd.FileName, FileMode.Open));
ofd.Title = "Select second Excel document";
if (ofd.ShowDialog() == DialogResult.OK)
{
HSSFWorkbook book2 = new HSSFWorkbook(new FileStream(ofd.FileName, FileMode.Open));
HSSFWorkbook product = new HSSFWorkbook();
for (int i = 0; i < book1.NumberOfSheets; i++)
{
HSSFSheet sheet1 = book1.GetSheetAt(i) as HSSFSheet;
sheet1.CopyTo(product, sheet1.SheetName, true, true);
}
for (int j = 0; j < book2.NumberOfSheets; j++)
{
HSSFSheet sheet2 = book2.GetSheetAt(j) as HSSFSheet;
sheet2.CopyTo(product, sheet2.SheetName, true, true);
}
product.Write(new FileStream("test.xls", FileMode.Create, FileAccess.ReadWrite));
}
}
}
}
}
我运行官方代码,结果也是报错,大家有没有好的解决方法呢 展开
2个回答
展开全部
这段代厅亩团码有一点小问题,就是当第二个工作簿中存在和第一个工作簿名字相同的工作表时出错,错误就在sheet2.copyto那句。
如果两个耐盯工作簿中没有名字相同的工扮橘作表则不会出错。
如果两个耐盯工作簿中没有名字相同的工扮橘作表则不会出错。
追问
这个代码是官方示例代码,备注上写着呢 Note:这里。
可是如果我两个工作簿都选择不同的,报错的是 product.Write 这句,感觉好奇怪。试过了好多种办法,HSSFWorkbook的insert,Add方法都试过了,没啥用。兄台有没有好办法呢,copyrow,copycell会改变原来的格式~~
追答
你重点检查一下product.write的错误提示,根据错误提示有针对性的解决。建议把错误提示贴出来。
注:上面的代码在我的机器上运行完全没有问题(只要两个工作簿中没有同名的sheet)。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询