亲宝软件园·资讯

展开

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

在代码的世界里游走 人气:0

使用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();

运行读取的结果:

加载全部内容

相关教程
猜你喜欢
用户评论