示例#1
0
        public override Dictionary <string, List <List <object> > > ReadFromExcels(string filename)
        {
            Dictionary <string, List <List <object> > > cache = new Dictionary <string, List <List <object> > >();

            ActionExcelRead(filename, workbook =>
            {
                foreach (var item in workbook.Sheets)
                {
                    var workSheet = (Worksheet)item;
                    Ldebug.Log(workSheet.Name);

                    var hangCount = workSheet.UsedRange.Cells.Rows.Count;
                    var lieCount  = workSheet.UsedRange.Cells.Columns.Count;

                    if (hangCount == 0 || lieCount == 0)
                    {
                        continue;
                    }
                    List <object[, ]> tempList = new List <object[, ]>();
                    for (int j = 0; j < lieCount; j++)
                    {
                        tempList.Add((object[, ])workSheet.Cells.Range[zimu[j] + "1", zimu[j] + hangCount].Value);
                    }


                    Dictionary <object, List <object> > dic = new Dictionary <object, List <object> >();
                    foreach (object[,] objectse in tempList)
                    {
                        if (objectse == null)
                        {
                            continue;
                        }
                        int index = 0;
                        foreach (object o in objectse)
                        {
                            index++;
                            List <object> res = null;
                            if (dic.TryGetValue(index, out res))
                            {
                                res.Add(o);
                            }
                            else
                            {
                                dic.Add(index, new List <object> {
                                    o
                                });
                            }
                        }
                    }

                    if (dic.Count == 0)
                    {
                        continue;
                    }
                    cache[workSheet.Name] = dic.Values.ToList();
                }
            });
            return(cache);
        }
示例#2
0
 public override void WriteToExcel(string filename, List <List <object> > vals)
 {
     //http://bbs.csdn.net/topics/390201171
     ActionExcelWrite(filename, workbook =>
     {
         var workSheet = (Worksheet)workbook.ActiveSheet;
         var f         = Path.GetFileNameWithoutExtension(filename);
         Ldebug.Log(workSheet.Name);
         //第一种:Excel.Application的Cell by Cell
         for (int j = 0; j < vals.Count; j++)
         {
             for (int i = 0; i < vals[j].Count; i++)
             {
                 workSheet.Cells[j + 1, i + 1] = vals[j][i];
             }
             Console.WriteLine(f + "/" + j + "/" + vals.Count);
         }
         workSheet = null;
     });
 }