从Excel工作簿中的txt文件传输数据

时间:2017-01-31 07:52:41

标签: c#

下午好。面对这个问题。有一项任务是在Excel中打开* .txt文件。

Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

if (xlApp == null)
{
    TaskDialog.Show("ups", "Where's your Excel?!");
}

Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;

object misValue = System.Reflection.Missing.Value;


xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

xlApp.Workbooks.OpenText
(
    @"C:\newPath\OveralFile.txt", 
    Excel.XlPlatform.xlWindows,
    1,            
    Excel.XlTextParsingType.xlDelimited,
    Excel.XlTextQualifier.xlTextQualifierDoubleQuote,
    true,          
    true,          
    false,         
    false,         
    false,         
    false,         
    Type.Missing,  
    misValue,
    Type.Missing,  
    ".",           
    ","
);

xlWorkBook.SaveAs(@"C:\newPath\csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);


xlWorkBook.Close(true, misValue, misValue);

xlApp.Quit();


Marshal.ReleaseComObject(xlWorkSheet);
Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(xlApp);

代码簿存在后,但是为空。 它看起来像一个文本文档。

enter image description here

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您的代码存在的问题是您没有保存已读取的工作簿,而是保存为空的工作簿。尝试更改此行:

xlWorkBook.SaveAs(@"C:\newPath\csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, 
                  misValue, misValue, misValue, misValue, 
                  Excel.XlSaveAsAccessMode.xlExclusive, misValue, 
                  misValue, misValue, misValue, misValue);

进入这个:

xlApp.Workbooks[2].SaveAs(@"C:\newPath\csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, 
                          misValue, misValue, misValue, misValue, 
                          Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, 
                          misValue, misValue, misValue);

这样你就可以保存新的readed工作簿了。

修改

我已经清理了你的代码,因为你正在创建不需要的对象而且它打开了Excel进程。代码应该是这样的:

Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
object misValue = System.Reflection.Missing.Value;

xlApp.Workbooks.OpenText
    (
    @"C:\newPath\OveralFile.txt",
    Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,
    1,
    Microsoft.Office.Interop.Excel.XlTextParsingType.xlDelimited,
    Microsoft.Office.Interop.Excel.XlTextQualifier.xlTextQualifierDoubleQuote,
    true,
    true,
    false,
    false,
    false,
    false,
    Type.Missing,
    misValue,
    Type.Missing,
    ".",
    ","
);

xlApp.Workbooks[1].SaveAs(@"C:\newPath\csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, 
                         misValue, misValue, misValue, misValue, 
                         Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, 
                         misValue, misValue);

xlApp.Workbooks[1].Close();

xlApp.Quit();

Marshal.ReleaseComObject(xlApp.Workbooks);

Marshal.ReleaseComObject(xlApp);