[英]How do I merge 2 Excel files into one excel file with separated sheets?
I've 2 Excel files, and I want to merge them into 1 file with separate sheets.. 我有2个Excel文件,我想将它们合并为1个带有单独工作表的文件。
I trying to perform the merging with Microsoft.Office.Interop.Excel, but I don't understand how to use that? 我试图执行与Microsoft.Office.Interop.Excel的合并,但是我不知道如何使用它?
for Yahia: 对于Yahia:
here the methods for get a Range that i want to merge them with different file: 在这里,我想要将它们与不同文件合并的Range方法:
internal object[,] GetValues(string filename)
{
object[,] values = new object[0, 0];
try
{
Workbook workBook = _excelApp.Workbooks.Open(filename,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
values = ExcelScanIntenal(workBook);
workBook.Close(false, filename, null);
Marshal.ReleaseComObject(workBook);
}
catch
{
}
return values;
}
private object[,] ExcelScanIntenal(Workbook workBookIn)
{
object[,] valueArray = new object[0, 0];
Worksheet sheet = (Worksheet)workBookIn.Sheets[1];
Range excelRange = sheet.UsedRange;
valueArray = (object[,])excelRange.get_Value(XlRangeValueDataType.xlRangeValueDefault);
return valueArray;
}
and here i want to merge the values: 在这里我想合并值:
internal void AddWorksheetToExcelWorkbook(string filename, string worksheetName, object[,] valueArray)
{
Microsoft.Office.Interop.Excel.Application xlApp = null;
Workbook xlWorkbook = null;
Sheets xlSheets = null;
Worksheet xlNewSheet = null;
try
{
xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
return;
xlWorkbook = xlApp.Workbooks.Open(filename, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
xlSheets = xlWorkbook.Sheets as Sheets;
xlNewSheet = (Worksheet)xlSheets.Add(xlSheets[1], Type.Missing, Type.Missing, Type.Missing);
xlNewSheet.Name = worksheetName;
xlWorkbook.Save();
xlWorkbook.Close(Type.Missing, Type.Missing, Type.Missing);
xlApp.Quit();
}
finally
{
Marshal.ReleaseComObject(xlNewSheet);
Marshal.ReleaseComObject(xlSheets);
Marshal.ReleaseComObject(xlWorkbook);
Marshal.ReleaseComObject(xlApp);
xlApp = null;
}
}
the problem is that xlNewSheet isn't have any property that can to get the values.. how can i to add?? 问题是xlNewSheet没有任何可以获取值的属性。我该如何添加?
Not really sure what the question is... but for merging excel files programmatically see for a starting point: 不太确定问题是什么...但是要以编程方式合并excel文件,请参阅以下内容:
IF you need more information then show some code please and tell what exactly isn't working... 如果您需要更多信息,请显示一些代码并告诉您确实不起作用...
EDIT - as per comment: 编辑-根据评论:
You can use xlNewSheet.Cells
to get a Range and use its Columns
/ Rows
/ Item
properties change/add any values you want - ie in your case from valueArray
... 您可以使用xlNewSheet.Cells
获取一个Range并使用其Columns
/ Rows
/ Item
属性更改/添加所需的任何值-例如,从valueArray
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.