1、利用c#将多个excel文件合并成一个excel文件
2、最好源程序,在网上下载的就算了。
3、悬赏20c币,各位大神显身手。
如何利用c#控制台应用程序将多个excel文件合并成一个excel文件?
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
4条回答 默认 最新
threenewbee 2019-05-10 12:51关注其它人如果也需要:https://download.csdn.net/download/caozhy/11173400
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Excel; namespace Q760776 { class Program { static void Main(string[] args) { string file1 = @"C:\Users\caozh\Desktop\book1.xls"; string file2 = @"C:\Users\caozh\Desktop\book2.xls"; string output = @"C:\Users\caozh\Desktop\book3.xls"; File.Copy(file1, output, true); Excel.Application app1 = new Excel.Application(); Excel.Workbook wb1 = app1.Workbooks.Open(output); Excel.Worksheet ws1 = (Excel.Worksheet)wb1.Sheets[1]; Excel.Application app2 = new Excel.Application(); Excel.Workbook wb2 = app2.Workbooks.Open(file2); Excel.Worksheet ws2 = (Excel.Worksheet)wb2.Sheets[1]; try { int offset = ws1.UsedRange.Rows.Count; for (int i = 0; i < ws2.UsedRange.Rows.Count; i++) { for (int j = 0; j < ws2.UsedRange.Columns.Count; j++) { var id1 = ((char)('A' + j)).ToString() + (i + 1 + offset).ToString(); var id2 = ((char)('A' + j)).ToString() + (i + 1).ToString(); ws1.Cells.Range[id1].Value2 = ws2.Cells.Range[id2].Value2; } } } finally { wb1.Save(); wb1.Close(); app1.Quit(); wb2.Close(); app2.Quit(); Process.GetProcessesByName("excel").Select(x => { x.Kill(); return 0; }); } } } }本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报解决 1无用
