当前位置:主页 > 软件编程 > .NET代码 >

C#使用Aspose.Cells创建和读取Excel文件

时间:2020-11-17 01:23:13 | 栏目:.NET代码 | 点击:

使用Aspose.Cells创建和读取Excel文件,供大家参考,具体内容如下

1. 创建Excel

Aspose.Cells.License li = new Aspose.Cells.License();
      li.SetLicense("Aspose.Cells.lic");
      Aspose.Cells.Workbook wk = new Aspose.Cells.Workbook();
      Worksheet ws = wk.Worksheets[0];
      for (int i = 0; i < 9; i++)
      {
        for (int j = 0; j < 9; j++)
        {
          ws.Cells.Rows[i][j].Value = (i + 1) + "*" + (j + 1) + "=" + (i + 1) * (j + 1);
        }
      }
      wk.Save("d:\\zzz\\d.xls");
      Console.WriteLine("ok");
      Console.ReadKey();产生的Excel文件:

2.读取Excel

DataTable dt = new DataTable();
      Aspose.Cells.License li = new Aspose.Cells.License();
      li.SetLicense("Aspose.Cells.lic");
      Aspose.Cells.Workbook wk = new Aspose.Cells.Workbook("d:\\zzz\\d.xls");
      Worksheet ws = wk.Worksheets[0];

      dt = ws.Cells.ExportDataTable(0, 0, 9, 9);

      for (int k = 0; k < dt.Rows.Count; k++)
      {
        for (int h = 0; h < dt.Columns.Count; h++)
        {
          Console.Write(dt.Rows[k][h].ToString() + " ");
        }
        Console.WriteLine();
      }
      Console.WriteLine("ok");
      Console.ReadKey();

运行读取的结果:

您可能感兴趣的文章:

相关文章