C#导出Excel

时间:2014-02-27 09:22:15   收藏:0   阅读:457

最近在做C#导出excel,虽然以也做过,但是数据量没有现在这么多的。

第一种方法就是以后组件的方式来做:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//public static void OutExcel(List<SubStationRealtimedata> listData, string str)
      //{
      //     Microsoft.Office.Interop.Excel.Application excelApp = new Application();
      //     Microsoft.Office.Interop.Excel.Workbook wbook = null;
      //     Microsoft.Office.Interop.Excel.Sheets sheets =null;
      //     Microsoft.Office.Interop.Excel.Worksheet wkSheet = null;
 
      //     Microsoft.Office.Interop.Excel.Range range = null;
      //     Microsoft.Office.Interop.Excel.Range rangeWire = null;
 
      //     wbook = excelApp.Workbooks.Add(true);
      //     sheets = wbook.Worksheets;
      //     wkSheet = (Worksheet)sheets.get_Item(1);
 
      //     System.Windows.Forms.Application.DoEvents();
 
      //     range = wkSheet.get_Range("A1", Missing.Value);
      //     rangeWire = wkSheet.get_Range("B1", Missing.Value);
      //     range.Value2 = "浙江";
      //     rangeWire.Value2 = "某某线";
 
      //     wkSheet.Cells[4, 1] = "时间";
      //     for (int i = 0; i < arrayList.Count; i++)
      //     {
      //         wkSheet.Cells[4, i + 2] = arrayList[i];
      //         int j=5;
      //         foreach(SubStationRealtimedata subdata in listData)
      //         {
      //             wkSheet.Cells[j, 1] = subdata.SendTime;
      //             wkSheet.Cells[j, i + 2] = subdata.CurrentPhaseA;
                    
      //             j++;
      //         }
               
      //     }
 
 
      //     wbook.Close();
 
      //     excelApp.Quit();
 
      //}

 由于以上种方法速度太慢,后来经过园子的网友推荐用npoi,这种流的方式效率非常高,网上先下载npoi组件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
SaveFileDialog dialog = new SaveFileDialog();
          dialog.Filter = "Excel文件|*.xls";
          if (dialog.ShowDialog() == DialogResult.No)
          {
               
          }
 
          string filename = dialog.FileName;
          HSSFWorkbook workbook = new HSSFWorkbook();
          ISheet sheet = workbook.CreateSheet("数据");
          //列表头
          IRow rowheader = sheet.CreateRow(4);
          //这里必须固定
          rowheader.CreateCell(0, CellType.String).SetCellValue("时间");
          for (int i = 1; i <= arrayList.Count; i++)
          {
              rowheader.CreateCell(i, CellType.String).SetCellValue(arrayList[i-1].ToString());
          }
 
          ArrayList arrayListData = null;
 
          //内容
          int j = 5;
          foreach (SubStationRealtimedata subdata in listData)
          {
 
              #region 先放入一个arraylist
              arrayListData = new ArrayList();
              if (pexcel != null)
              {
                  if (pexcel.PCurrentA)
                  {
                      arrayListData.Add(subdata.CurrentPhaseA);
                  }
                  if (pexcel.PCurrentB)
                  {
                      arrayListData.Add(subdata.CurrentPhaseB);
                  }
                  if (pexcel.PCurrentC)
                  {
                      arrayListData.Add(subdata.CurrentPhaseB);
                  }
                  if (pexcel.PVoltageA)
                  {
                      arrayListData.Add(subdata.CurrentPhaseB);
                  }
                  if (pexcel.PVoltageB)
                  {
                      arrayListData.Add(subdata.CurrentPhaseB);
                  }
                  if (pexcel.PVoltageC)
                  {
                      arrayListData.Add(subdata.CurrentPhaseB);
                  }
                  if (pexcel.PharmonicPrecent3)
                  {
                      arrayListData.Add(subdata.harmonicPrecent3);
                  }
                  if (pexcel.PharmonicPrecent5)
                  {
                      arrayListData.Add(subdata.harmonicPrecent5);
                  }
                  if (pexcel.PharmonicPrecent7)
                  {
                      arrayListData.Add(subdata.harmonicPrecent7);
                  }
                  if (pexcel.PharmonicPrecent9)
                  {
                      arrayListData.Add(subdata.harmonicPrecent9);
                  }
                  if (pexcel.PharmonicPrecent11)
                  {
                      arrayListData.Add(subdata.harmonicPrecent11);
                  }
                  if (pexcel.PharmonicPrecent13)
                  {
                      arrayListData.Add(subdata.harmonicPrecent13);
                  }
                  if (pexcel.PActive)
                  {
                      arrayListData.Add(subdata.Active);
                  }
                  if (pexcel.PReactive)
                  {
                      arrayListData.Add(subdata.Reactive);
                  }
              }
              #endregion
 
              IRow rowcontent = sheet.CreateRow(j);
              rowcontent.CreateCell(0, CellType.String).SetCellValue(subdata.SendTime);
              for (int i = 1; i <= arrayListData.Count; i++)
              {
                  rowcontent.CreateCell(i, CellType.String).SetCellValue(arrayListData[i-1].ToString());
              }
              j++;
          }
 
 
     //最后输出流就可以了。呵呵!非常方便。
          using (Stream stream = File.OpenWrite(filename))
          {
              workbook.Write(stream);
          }

  

C#导出Excel,布布扣,bubuko.com

原文:http://www.cnblogs.com/Anders888/p/3569317.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!