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

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

1. 创建Excel

 1             Aspose.Cells.License li = new Aspose.Cells.License();
 2             li.SetLicense("Aspose.Cells.lic");
 3             Aspose.Cells.Workbook wk = new Aspose.Cells.Workbook();
 4             Worksheet ws = wk.Worksheets[0];
 5             for (int i = 0; i < 9; i++)
 6             {
 7                 for (int j = 0; j < 9; j++)
 8                 {
 9                     ws.Cells.Rows[i][j].Value = (i + 1) + "*" + (j + 1) + "=" + (i + 1) * (j + 1);
10                 }
11             }
12             wk.Save("d:\\zzz\\d.xls");
13             Console.WriteLine("ok");
14             Console.ReadKey();

产生的Excel文件:

 

 

2.读取Excel

 1             DataTable dt = new DataTable();
 2             Aspose.Cells.License li = new Aspose.Cells.License();
 3             li.SetLicense("Aspose.Cells.lic");
 4             Aspose.Cells.Workbook wk = new Aspose.Cells.Workbook("d:\\zzz\\d.xls");
 5             Worksheet ws = wk.Worksheets[0];
 6 
 7             dt = ws.Cells.ExportDataTable(0, 0, 9, 9);
 8 
 9             for (int k = 0; k < dt.Rows.Count; k++)
10             {
11                 for (int h = 0; h < dt.Columns.Count; h++)
12                 {
13                     Console.Write(dt.Rows[k][h].ToString() + " ");
14                 }
15                 Console.WriteLine();
16             }
17             Console.WriteLine("ok");
18             Console.ReadKey();

运行读取的结果:

posted on 2018-10-19 09:15  在代码的世界里游走  阅读(3236)  评论(0编辑  收藏  举报