首页 新闻 会员 周边 捐助

C#后台返回数据直接导出excel

0
[已解决问题] 解决于 2017-09-05 15:56

后台返回json,怎么直接excel导出

haodejiuzheyangba的主页 haodejiuzheyangba | 初学一级 | 园豆:142
提问于:2017-09-04 16:01
< >
分享
最佳答案
0
public static void dataTableToExcel(DataTable table, string fileName)
        {
            using (MemoryStream ms = new MemoryStream())
            {

                IWorkbook workbook = null;

                if (fileName.IndexOf(".xlsx") > 0)
                    workbook = new XSSFWorkbook();
                if (fileName.IndexOf(".xls") > 0)
                    workbook = new HSSFWorkbook();
                ISheet sheet = workbook.CreateSheet();
                IRow headerRow = sheet.CreateRow(0);

                // handling header.  
                foreach (DataColumn column in table.Columns)
                    headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption);//If Caption not set, returns the ColumnName value  

                // handling value.  
                int rowIndex = 1;

                foreach (DataRow row in table.Rows)
                {
                    IRow dataRow = sheet.CreateRow(rowIndex);

                    foreach (DataColumn column in table.Columns)
                    {
                        dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
                    }

                    rowIndex++;
                }

                workbook.Write(ms);
                ms.Flush();

                using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
                {
                    byte[] data = ms.ToArray();

                    fs.Write(data, 0, data.Length);
                    fs.Flush();

                    data = null;
                }
            }
        }

NPOI下载地址:http://npoi.codeplex.com/releases/view/38113

Example:

DataTableToExcel.dataTableToExcel(dataTable, "D:\\Test.xlsx");
奖励园豆:5
~扎克伯格 | 小虾三级 |园豆:1923 | 2017-09-05 15:54

谢谢你

haodejiuzheyangba | 园豆:142 (初学一级) | 2017-09-05 15:55

@cai先sen是什么: 可以直接遍历json,不用非要先转成DataTable。

jsonToExcel Example:

 1 List<UserInfo> userInfoList = new List<UserInfo>();
 2             for (int i = 0; i < 10; i++)
 3             {
 4                 UserInfo userInfo = new UserInfo();
 5                 userInfo.UserNo = 1 + i;
 6                 userInfo.UserName = "Test" + i;
 7                 userInfo.Password = 0101 + i;
 8 
 9                 userInfoList.Add(userInfo);
10             }
11 
12             JavaScriptSerializer js = new JavaScriptSerializer();
13             string json = js.Serialize(userInfoList);
14             var jarr = js.Deserialize<List<Dictionary<string, object>>>(json);
15 
16             ExcelUtility.JsonToExcel(jarr, "D:\\JsonTest.xls");
17 
18 
19 
20 
21 
22         }
23         public class UserInfo
24         {
25             public int UserNo { get; set; }
26 
27             public string UserName { get; set; }
28 
29             public long Password { get; set; }
30         }
31 
32 
33 public static void JsonToExcel(List<Dictionary<string, object>> json, string fileName)
34         {
35             using (MemoryStream ms = new MemoryStream())
36             {
37 
38                 IWorkbook workbook = null;
39 
40                 if (fileName.IndexOf(".xlsx") > 0)
41                     workbook = new XSSFWorkbook();
42                 if (fileName.IndexOf(".xls") > 0)
43                     workbook = new HSSFWorkbook();
44                 ISheet sheet = workbook.CreateSheet();
45                 IRow headerRow = sheet.CreateRow(0);
46 
47                 //// handling header.  
48                 //foreach (DataColumn column in table.Columns)
49                 //    headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption);//If Caption not set, returns the ColumnName value  
50 
51 
52                 var keys = json.FirstOrDefault().Keys.ToList();
53                 for (var i = 0; i < keys.Count(); i++)
54                 {
55                     headerRow.CreateCell(i).SetCellValue(keys[i]);
56                 }
57 
58                 // handling value.  
59                 int rowIndex = 1;
60                 
61                 for (var i = 0; i < json.Count(); i++)
62                 {
63                     IRow dataRow = sheet.CreateRow(rowIndex);
64 
65                     var values=json[i].Values.ToList();
66                     for (var j = 0; j <values.Count();j++)
67                     {
68                         dataRow.CreateCell(j).SetCellValue(values[j].ToString());
69                     }
70 
71                     rowIndex++;
72                 }
73 
74                 workbook.Write(ms);
75                 ms.Flush();
76 
77                 using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
78                 {
79                     byte[] data = ms.ToArray();
80 
81                     fs.Write(data, 0, data.Length);
82                     fs.Flush();
83 
84                     data = null;
85                 }
86             }
87         }

 

~扎克伯格 | 园豆:1923 (小虾三级) | 2017-09-05 15:58
其他回答(2)
0

把JSON转成datatable或者list,进行循环,将对应字段的值赋值给对应的EXCEL单元格,注意索引是从0开始的~

SUKHOIIII | 园豆:11 (初学一级) | 2017-09-04 16:14
0

用Newtonsoft.Json 转成datatable,在用NPOI导出Excel

jqw2009 | 园豆:2341 (老鸟四级) | 2017-09-04 16:36

已经转成datatable,不知道怎么一行行放到excel里面

支持(0) 反对(0) haodejiuzheyangba | 园豆:142 (初学一级) | 2017-09-04 16:37

@cai先sen是什么: 用NPOI导出,不知道怎么用可以百度一下

支持(0) 反对(0) jqw2009 | 园豆:2341 (老鸟四级) | 2017-09-04 16:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册