我有两个Excel文件Excel1和Excel2。
我想将Excel1和Excel2的所有行复制到新的Excel3单张纸中。
我尝试了以下代码,但将所有Excel1和Excel2复制到单个Excel文件的两个不同表中。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Excel = Microsoft.Office.Interop.Excel;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
Excel.Application app = new Excel.Application();
app.Visible = true;
foreach (HttpPostedFile postedFile in FileUpload1.PostedFiles)
{
app.Workbooks.Add(@"C:\Users\Zest\Documents\" + postedFile.FileName);
}
for (int i = 2; i <= app.Workbooks.Count; i++)
{
for (int j = 1; j <= app.Workbooks[i].Worksheets.Count; j++)
{
Excel.Worksheet ws = (Excel.Worksheet)app.Workbooks[i].Worksheets[j];
ws.Copy(app.Workbooks[1].Worksheets[1]);
}
}
app.Workbooks[1].SaveCopyAs(@"D:\New folder\result.xlsx");
app.Quit();
}
}
}
我是C#的新手,请帮助我解决此问题。