如何将数据表导出到具有来自C#的Excel工作表的用户定义的行号的Excel工作表? [英] How can I Export a datatable to an excel sheet with user-defined row no of the excel sheet from c#?

查看:51
本文介绍了如何将数据表导出到具有来自C#的Excel工作表的用户定义的行号的Excel工作表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从C#将数据表导出到Excel工作表的用户定义行号的Excel工作表?

How can I Export a datatable to an excel sheet with user-defined row no of the excel sheet from c#?

推荐答案

我尝试了这个,我得到了答案.对于那些否定了我的问题的人,有可能做到这一点.首先,您应该考虑路线和工作方式

I tried this one I got the answer. For those who downvoted my question, there is a possible to do the way. First you should think the route and work

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工作表的用户定义的行号的Excel工作表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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