怎么用C#将txt文件的转换成excel文件,txt每格数据用逗号隔开,转换成功后也分别插入excel 中每一格中。 20
展开全部
不知道下面的代码能不能帮到你,你可以试试看
// Open the text file in Excel.
m_objExcel = new Excel.Application();
m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
m_objBooks.OpenText(m_strSampleFolder + "Book6.txt", Excel.XlPlatform.xlWindows, 1,
Excel.XlTextParsingType.xlDelimited, Excel.XlTextQualifier.xlTextQualifierDoubleQuote,
false, true, false, false, false, false, m_objOpt, m_objOpt,
m_objOpt, m_objOpt, m_objOpt);
m_objBook = m_objExcel.ActiveWorkbook;
// Save the text file in the typical workbook format and quit Excel.
m_objBook.SaveAs(m_strSampleFolder + "Book6.xls", Excel.XlFileFormat.xlWorkbookNormal,
m_objOpt, m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange, m_objOpt, m_objOpt,
m_objOpt, m_objOpt);
m_objBook.Close(false, m_objOpt, m_objOpt);
m_objExcel.Quit();
// Open the text file in Excel.
m_objExcel = new Excel.Application();
m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
m_objBooks.OpenText(m_strSampleFolder + "Book6.txt", Excel.XlPlatform.xlWindows, 1,
Excel.XlTextParsingType.xlDelimited, Excel.XlTextQualifier.xlTextQualifierDoubleQuote,
false, true, false, false, false, false, m_objOpt, m_objOpt,
m_objOpt, m_objOpt, m_objOpt);
m_objBook = m_objExcel.ActiveWorkbook;
// Save the text file in the typical workbook format and quit Excel.
m_objBook.SaveAs(m_strSampleFolder + "Book6.xls", Excel.XlFileFormat.xlWorkbookNormal,
m_objOpt, m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange, m_objOpt, m_objOpt,
m_objOpt, m_objOpt);
m_objBook.Close(false, m_objOpt, m_objOpt);
m_objExcel.Quit();
参考资料: http://support.microsoft.com/kb/306023
展开全部
以微软的office为例(电脑中必须安装有office excel):
你会用c#把txt文件的内容读出来吗?
如果不会看一下 StreamReader 类
读出来以后,在项目中添加引用,选.NET中的Microsoft.Office.Interop.Excel
再看一下 该命名空间下的类:Application,_Workbook,Sheets,_Worksheet,Range
最后一定要手动清理资源。
我只把清理资源的代码贴上:
book_excel.Close(false, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
System.Runtime.InteropServices.Marshal.ReleaseComObject(range_excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheets_excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(book_excel);
excel_1.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel_1);
祝你好运。
你会用c#把txt文件的内容读出来吗?
如果不会看一下 StreamReader 类
读出来以后,在项目中添加引用,选.NET中的Microsoft.Office.Interop.Excel
再看一下 该命名空间下的类:Application,_Workbook,Sheets,_Worksheet,Range
最后一定要手动清理资源。
我只把清理资源的代码贴上:
book_excel.Close(false, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
System.Runtime.InteropServices.Marshal.ReleaseComObject(range_excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheets_excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(book_excel);
excel_1.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel_1);
祝你好运。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询