将某一个多于30行的文件都写入到excel的第一行第一列中,再从中读出内容写到另外
一个文件,可是发现能将文件的内容写入到excel中,但从excel中往外写总是写道20多
行就不写了,为什么啊using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;using System.Collections;using System.Reflection; namespace Test{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void WriteRoTxt() { string OrignfileName =
System.Windows.Forms.Application.StartupPath + "\\" + "test.xls";//
"Book1.xls" Excel.Application xlApp = null; Excel.Workbook xlbook = null; Excel.Worksheet xlsheet = null; System.Reflection.Missing oMissing =
System.Reflection.Missing.Value; try { xlApp = new Excel.ApplicationClass(); xlApp.Visible = false; xlbook = xlApp.Workbooks._Open(OrignfileName, Missing.Value, Missing.Value, Missing.Value,
Missing.Value , Missing.Value, Missing.Value, Missing.Value,
Missing.Value , Missing.Value, Missing.Value, Missing.Value,
Missing.Value); xlsheet = (Excel.Worksheet)xlbook.Sheets[1]; Excel.Range rng2 = (Excel.Range)xlsheet.Cells[1, 1]; String a1 = (String)rng2.Text; FileInfo myFile22 = new FileInfo("a.txt"); StreamWriter sw2 = myFile22.CreateText(); sw2.WriteLine(a1);// sw2.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally {
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp); xlsheet = null; xlbook = null; xlApp = null; GC.Collect(); //KillProcess("EXCEL");// } } private void button1_Click(object sender, EventArgs e) { Excel.Application xlApp = null; Excel.Workbook xlWorkbook = null; Excel.Worksheet xlWorksheet = null; string sLine = ""; string sRigth = ""; System.Reflection.Missing oMissing =
System.Reflection.Missing.Value; string saveAsPath = ""; try { xlApp = new Excel.Application(); xlApp.Visible = false; xlWorkbook = xlApp.Workbooks.Add(oMissing); xlWorksheet = xlWorkbook.Worksheets.Add(oMissing, oMissing,
1, oMissing) as Excel.Worksheet; xlWorksheet.Name = "test"; StreamReader objReader = new
StreamReader("e:\\11ErrorListView.cpp"); int text = 0; while (sLine != null) { text++; if(text > 25) { sLine = objReader.ReadLine(); sRigth += "\r\n" + sLine; } } xlWorksheet.Cells[1, 1] = sRigth; saveAsPath = System.Windows.Forms.Application.StartupPath +
"\\" + "test"; FileInfo TheFile = new FileInfo(saveAsPath + ".xls"); if (TheFile.Exists) { TheFile.Delete(); } xlWorkbook.SaveAs(saveAsPath, oMissing, oMissing, oMissing,
oMissing, oMissing, Excel.XlSaveAsAccessMode.xlShared, oMissing,
oMissing, oMissing, oMissing, oMissing); xlApp.DisplayAlerts = false; xlApp.AlertBeforeOverwriting = false; //x.Cells[1, 1] = "note is:"; xlWorkbook.Save(); xlApp.Quit(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally {
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp); xlWorksheet = null; xlWorkbook = null; xlApp = null; GC.Collect(); } WriteRoTxt(); } }}
一个文件,可是发现能将文件的内容写入到excel中,但从excel中往外写总是写道20多
行就不写了,为什么啊using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;using System.Collections;using System.Reflection; namespace Test{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void WriteRoTxt() { string OrignfileName =
System.Windows.Forms.Application.StartupPath + "\\" + "test.xls";//
"Book1.xls" Excel.Application xlApp = null; Excel.Workbook xlbook = null; Excel.Worksheet xlsheet = null; System.Reflection.Missing oMissing =
System.Reflection.Missing.Value; try { xlApp = new Excel.ApplicationClass(); xlApp.Visible = false; xlbook = xlApp.Workbooks._Open(OrignfileName, Missing.Value, Missing.Value, Missing.Value,
Missing.Value , Missing.Value, Missing.Value, Missing.Value,
Missing.Value , Missing.Value, Missing.Value, Missing.Value,
Missing.Value); xlsheet = (Excel.Worksheet)xlbook.Sheets[1]; Excel.Range rng2 = (Excel.Range)xlsheet.Cells[1, 1]; String a1 = (String)rng2.Text; FileInfo myFile22 = new FileInfo("a.txt"); StreamWriter sw2 = myFile22.CreateText(); sw2.WriteLine(a1);// sw2.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally {
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp); xlsheet = null; xlbook = null; xlApp = null; GC.Collect(); //KillProcess("EXCEL");// } } private void button1_Click(object sender, EventArgs e) { Excel.Application xlApp = null; Excel.Workbook xlWorkbook = null; Excel.Worksheet xlWorksheet = null; string sLine = ""; string sRigth = ""; System.Reflection.Missing oMissing =
System.Reflection.Missing.Value; string saveAsPath = ""; try { xlApp = new Excel.Application(); xlApp.Visible = false; xlWorkbook = xlApp.Workbooks.Add(oMissing); xlWorksheet = xlWorkbook.Worksheets.Add(oMissing, oMissing,
1, oMissing) as Excel.Worksheet; xlWorksheet.Name = "test"; StreamReader objReader = new
StreamReader("e:\\11ErrorListView.cpp"); int text = 0; while (sLine != null) { text++; if(text > 25) { sLine = objReader.ReadLine(); sRigth += "\r\n" + sLine; } } xlWorksheet.Cells[1, 1] = sRigth; saveAsPath = System.Windows.Forms.Application.StartupPath +
"\\" + "test"; FileInfo TheFile = new FileInfo(saveAsPath + ".xls"); if (TheFile.Exists) { TheFile.Delete(); } xlWorkbook.SaveAs(saveAsPath, oMissing, oMissing, oMissing,
oMissing, oMissing, Excel.XlSaveAsAccessMode.xlShared, oMissing,
oMissing, oMissing, oMissing, oMissing); xlApp.DisplayAlerts = false; xlApp.AlertBeforeOverwriting = false; //x.Cells[1, 1] = "note is:"; xlWorkbook.Save(); xlApp.Quit(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally {
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp); xlWorksheet = null; xlWorkbook = null; xlApp = null; GC.Collect(); } WriteRoTxt(); } }}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货