简体   繁体   English

如何从C#保存到Excel?

[英]How to save from C# to Excel?

BOWorkerDetail oBOWorkerDetail = new BOWorkerDetail();
            WorkerDetails oWorkerDetails = new WorkerDetails();
            oWorkerDetails = oBOWorkerDetail.Gets();

oWorkerdetails is a collection of all workers. oWorkerdetails是所有工人的集合。 I need to save this value in Excel. 我需要将此值保存在Excel中。 how to do ? 怎么做 ? Can any one help?i Work on C# window platform vs05. 有人可以帮忙吗?i在C#窗口平台vs05上工作。

you should look at this http://www.connectionstrings.com/excel , there you can find some connection strings that works with ODBC engine. 您应该看一下http://www.connectionstrings.com/excel ,在那里您可以找到一些与ODBC引擎一起使用的连接字符串。 An example is: 一个例子是:

OdbcConnection connection = new OdbcConnection(@"Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=" + xlsFilePath + "; ReadOnly=False; DefaultDir=" + xlsDir + ";");

(You should have Microsoft Excel installed on the workstation) (您应该在工作站上安装Microsoft Excel)

then you can create a IDbCommand like this: IDbCommand command = connection.CreateCommand(); 那么您可以像这样创建一个IDbCommand:IDbCommand command = connection.CreateCommand();

you can use this command like any sql command : "CREATE TABLE" , "INSET INTO" for each of DataRow in your DataTable. 您可以像使用任何sql命令一样使用此命令:为DataTable中的每个DataRow使用“ CREATE TABLE”,“ INSET INTO”。

Hope this helps... 希望这可以帮助...

My recomendation is to use a native library and avoid Interop. 我的建议是使用本机库,避免使用Interop。 Take a look at Aspose.Cells 看看Aspose.Cells

If you don't have Excel installed on the machine running the code you can export as XML or CSV (Comma Separated Values) file and then import that in to Excel at a later date. 如果没有在运行该代码的计算机上安装Excel,则可以将其导出为XML或CSV(逗号分隔值)文件,然后在以后将其导入Excel。

If you use XML you can export the schema as well. 如果使用XML,则也可以导出模式。

As BarneyHDog points out in the comments, Excel can do nasty to things to data based on what it thinks the data type. 正如BarneyHDog在注释中指出的那样,Excel可以根据其认为的数据类型对数据进行处理。 So if you go down this route double check that your output is handled correctly. 因此,如果沿着这条路线走,请仔细检查输出是否正确处理。

Here is an example of using Visual Studio Tools for Office 2008 and Office Excel Interop to write a List> to new Excel file: 这是一个使用Visual Studio Tools for Office 2008和Office Excel Interop将List>写入新Excel文件的示例:

using System;
using System.Collections.Generic;
using System.Reflection;

using Microsoft.Office.Interop.Excel;

namespace Project1
{
  public class ExportExcel
  {
    public void Export(string fileName, List<List<string>> data)
    {
      _Application excel = null;
      _Workbook wb = null;
      try
      {
        excel = new ApplicationClass { Visible = false }; // new excel application (not visible)
        wb = excel.Workbooks.Add(Missing.Value); // new excel file

        var sh = ((_Worksheet)wb.ActiveSheet); // current sheet
        for (var i = 0; i < data.Count; i++)
        {
          var listMaster = data[i];
          for (var j = 0; j < listMaster.Count; j++)
          {
            sh.get_Range(sh.Cells[j + 1, i + 1], sh.Cells[j + 1, i + 1]).Value2 = listMaster[j];
            // get_Range(start, end) where start, end is in R1C1 notation
          }
        }
      }
      finally
      {
        if (wb != null)
        {
          wb.SaveAs(fileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
        }
        if (excel != null)
        {
          excel.Quit();
        }
        GC.Collect();
      }
    }
  }
}

VSTO requires Excel to be installed on client machine VSTO要求在客户端计算机上安装Excel

A really simple way of doing this is using the filehelpers library 一种非常简单的方法是使用filehelpers库

http://filehelpers.sourceforge.net/ http://filehelpers.sourceforge.net/

there are loads of examples there. 那里有很多例子。

The library is really reliable, I ve used it many times. 该库确实可靠,我已经使用了很多次。 Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM