求教--- 如何用C#将TXT文件中的数据导入到EXCEL中?
各位大侠, 我想把TXT文件里的数据导入到EXCEL里,那位搞过这方面的东东,请赐教。。。。。。本人初学, 如果有好的关于这方面操作的书,麻烦推荐, 先谢了。
private static Excel.Application myExcel;//Excel主程序 private static Excel.Workbooks myBooks;// private static Excel.Workbook myBook;// private static Excel.Worksheet mySheet;//工作薄 private static Excel.Range myRange; private static object miss = Type.Missing;//你要是用的VS2011就不用这个了打开文件
void OpenExcelFile(string name) { myExcel.Workbooks.Open(name, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss); //myExcel.Workbooks.Open(Filename: name);VS2011可以这样写,方便很多 myBooks = (Excel.Workbooks)myExcel.Workbooks; myBook = myExcel.ActiveWorkbook; mySheet = (Excel.Worksheet)myExcel.ActiveSheet; }
int start = 64; object range = null; for (int i = 2; i <= mySheet.UsedRange.Rows.Count; i++) { for (int j = 1; j <= mySheet.UsedRange.Columns.Count; j++) { try { //这是取Range,WORD和EXCEL里面RANGE都是一个很重要的模型 range = ((char)(start + j)).ToString() + i.ToString(); myRange = mySheet.get_Range(range, miss); //或者直接用Cell mySheet.Cells[((char)(start + j)).ToString(), i] = "...."; } catch { } } }