我们如何从C#中写入Excel的特定行,而不是整个行中的单元格 [英] How can we write a particular row of the excel from C# , not to a cell the whole row

查看:155
本文介绍了我们如何从C#中写入Excel的特定行,而不是整个行中的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何从c#写入excel的特定行,而不是整个行中的单元格.我试图写一个单元格但是写一行却失败了

我尝试过的事情:

How can we write a particular row of the excel from c# , not to a cell the whole row. i have tried to write to a cell but to a row i failed to write

What I have tried:

private void button2_Click(object sender, EventArgs e)
        {
            MyExcel.Application excelApp;
            MyExcel.Workbook excelWorkBook;
            MyExcel.Worksheet excelWorkSheet;
            MyExcel.Range range;

            string str;
            int rowCount = 0;
            int colCount = 0;

            excelApp = new MyExcel.Application();
            excelWorkBook = excelApp.Workbooks.Open("e:\\SampleFile.xls", 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            excelWorkSheet = (MyExcel.Worksheet)excelWorkBook.Worksheets.get_Item(1);
 

            range = excelWorkSheet.UsedRange;

            for (rowCount = 1; rowCount <= range.Rows.Count; rowCount++)
            {
                for (colCount = 1; colCount <= range.Columns.Count; colCount++)
                {
                    str = (string)(range.Cells[rowCount, colCount] as MyExcel.Range).Value2;
                    MessageBox.Show(str);
                }
            }
            excelWorkSheet.Cells[rowCount, 1] = "new name";
            excelWorkBook.Save();
            excelWorkBook.Close();
            excelApp.Quit();

            releaseObject(excelWorkSheet);
            releaseObject(excelWorkBook);
            releaseObject(excelApp);
        }

推荐答案

替换为:
Replace this:
for (rowCount = 1; rowCount <= range.Rows.Count; rowCount++)
{
    for (colCount = 1; colCount <= range.Columns.Count; colCount++)
    {
        str = (string)(range.Cells[rowCount, colCount] as MyExcel.Range).Value2;
        MessageBox.Show(str);
    }
}


与:


with:

int r = 1; 
int rowCount = 10;
int c = 1; 
int colCount = 10;
//multiplication table
for (r = 1; r <= rowCount; r++)
{
    for (c = 1; c <= colCount; c++)
    {
        range.Cells[rowCount, colCount].Value = r*c;
        //MessageBox.Show(range.Cells[rowCount, colCount].Value);
    }
}


private static Microsoft.Office.Interop.Excel.Workbook mWorkBook;
private static Microsoft.Office.Interop.Excel.Sheets mWorkSheets;
private static Microsoft.Office.Interop.Excel.Worksheet mWSheet1;
private static Microsoft.Office.Interop.Excel.Application oXL;
public static void ReadExistingExcel()
{
   string path = @"C:\Tool\Reports1.xls";
   oXL = new Microsoft.Office.Interop.Excel.Application();
   oXL.Visible = true;
   oXL.DisplayAlerts = false;
   mWorkBook = oXL.Workbooks.Open(path, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
   //Get all the sheets in the workbook
  mWorkSheets = mWorkBook.Worksheets;
   //Get the allready exists sheet
   mWSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)mWorkSheets.get_Item("Sheet1");
   Microsoft.Office.Interop.Excel.Range range= mWSheet1.UsedRange;
   int colCount = range.Columns.Count;
   int rowCount= range.Rows.Count;
   for (int index = 1; index < 15; index++)
   {
      mWSheet1.Cells[rowCount + index, 1] = rowCount +index;
      mWSheet1.Cells[rowCount + index, 2] = "New Item"+index;
   }
   mWorkBook.SaveAs(path, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,
   Missing.Value, Missing.Value, Missing.Value,    Missing.Value,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
   Missing.Value, Missing.Value, Missing.Value,
   Missing.Value, Missing.Value);
   mWorkBook.Close(Missing.Value, Missing.Value, Missing.Value);
   mWSheet1 = null;
   mWorkBook = null;
   oXL.Quit();
   GC.WaitForPendingFinalizers();
   GC.Collect();
   GC.WaitForPendingFinalizers();
   GC.Collect();
} 


这篇关于我们如何从C#中写入Excel的特定行,而不是整个行中的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆