﻿{"id":6785,"date":"2018-11-06T09:24:36","date_gmt":"2018-11-06T01:24:36","guid":{"rendered":"http:\/\/www.yeyaorong.cn\/?p=6785"},"modified":"2018-11-06T15:20:07","modified_gmt":"2018-11-06T07:20:07","slug":"vs2017%e7%94%a8nuget%e5%ae%89%e8%a3%85npoi%e5%ae%9e%e7%8e%b0datatable%e5%af%bc%e5%87%ba%e5%88%b0excel%e5%b9%b6%e4%b8%8b%e8%bd%bd","status":"publish","type":"post","link":"http:\/\/www.yeyaorong.cn\/?p=6785","title":{"rendered":"VS2017\u7528nuget\u5b89\u88c5NPOI\u5b9e\u73b0datatable\u5bfc\u51fa\u5230excel\u5e76\u4e0b\u8f7d"},"content":{"rendered":"<p>1.\u7528nuget\u547d\u4ee4\u884c\u5b89\u88c5NPOI\u5f15\u7528\uff1a<\/p>\n<p><span style=\"color: #ff0000;\">Install-Package NPOI -Version 2.4.0<\/span>\uff1b<\/p>\n<p>2.\u65b9\u6cd5\uff1a<\/p>\n<p><span style=\"color: #0000ff;\">public void ToExcelFile(string filename, DataTable dt, KeyValuePair&lt;string, string&gt;[] column_names)<\/span><br \/>\n<span style=\"color: #0000ff;\">{<\/span><br \/>\n<span style=\"color: #0000ff;\">try<\/span><br \/>\n<span style=\"color: #0000ff;\">{<\/span><br \/>\n<span style=\"color: #0000ff;\">\/\/ \u6839\u636e\u6a21\u677f\u6587\u4ef6\u521b\u5efa\u526f\u672c<\/span><br \/>\n<span style=\"color: #0000ff;\">\/\/string filePath = Server.MapPath(this.TemporaryFolder + Path.DirectorySeparatorChar + Guid.NewGuid().ToString() + &#8220;.xls&#8221;);<\/span><br \/>\n<span style=\"color: #0000ff;\">MemoryStream ms = new MemoryStream();<\/span><br \/>\n<span style=\"color: #0000ff;\">var book = new HSSFWorkbook();<\/span><br \/>\n<span style=\"color: #0000ff;\">var sheet = book.CreateSheet(Path.GetFileNameWithoutExtension(filename));<\/span><br \/>\n<!--more--><br \/>\n<span style=\"color: #0000ff;\">var row_h = sheet.CreateRow(0);<\/span><br \/>\n<span style=\"color: #0000ff;\">var style_h = book.CreateCellStyle();<\/span><br \/>\n<span style=\"color: #0000ff;\">for (int i = 0; i &lt; column_names.Length; i++)<\/span><br \/>\n<span style=\"color: #0000ff;\">{<\/span><br \/>\n<span style=\"color: #0000ff;\">var cell = row_h.CreateCell(i, NPOI.SS.UserModel.CellType.String);<\/span><br \/>\n<span style=\"color: #0000ff;\">cell.SetCellValue(column_names[i].Value);<\/span><br \/>\n<span style=\"color: #0000ff;\">style_h.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index;<\/span><br \/>\n<span style=\"color: #0000ff;\">style_h.FillPattern = NPOI.SS.UserModel.FillPattern.FineDots;<\/span><br \/>\n<span style=\"color: #0000ff;\">style_h.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index;<\/span><br \/>\n<span style=\"color: #0000ff;\">cell.CellStyle = style_h;<\/span><br \/>\n<span style=\"color: #0000ff;\">}<\/span><br \/>\n<span style=\"color: #0000ff;\">for (int i = 0; i &lt; dt.Rows.Count; i++)<\/span><br \/>\n<span style=\"color: #0000ff;\">{<\/span><br \/>\n<span style=\"color: #0000ff;\">var row = sheet.CreateRow(i + 1);<\/span><br \/>\n<span style=\"color: #0000ff;\">for (int j = 0; j &lt; column_names.Length; j++)<\/span><br \/>\n<span style=\"color: #0000ff;\">{<\/span><br \/>\n<span style=\"color: #0000ff;\">NPOI.SS.UserModel.ICell cell = null;<\/span><\/p>\n<p><span style=\"color: #0000ff;\">if (dt.Columns[column_names[j].Key].DataType == typeof(decimal) || dt.Columns[column_names[j].Key].DataType == typeof(Int64) || dt.Columns[column_names[j].Key].DataType == typeof(Int32) || dt.Columns[column_names[j].Key].DataType == typeof(Int16) || dt.Columns[column_names[j].Key].DataType == typeof(double) || dt.Columns[column_names[j].Key].DataType == typeof(float))<\/span><br \/>\n<span style=\"color: #0000ff;\">{<\/span><br \/>\n<span style=\"color: #0000ff;\">cell = row.CreateCell(j, NPOI.SS.UserModel.CellType.Numeric);<\/span><br \/>\n<span style=\"color: #0000ff;\">if (!(dt.Rows[i][column_names[j].Key] == DBNull.Value || dt.Rows[i][column_names[j].Key] == null))<\/span><br \/>\n<span style=\"color: #0000ff;\">{<\/span><br \/>\n<span style=\"color: #0000ff;\">cell.SetCellValue((double)decimal.Parse(dt.Rows[i][column_names[j].Key].ToString()));<\/span><br \/>\n<span style=\"color: #0000ff;\">}<\/span><br \/>\n<span style=\"color: #0000ff;\">}<\/span><br \/>\n<span style=\"color: #0000ff;\">else<\/span><br \/>\n<span style=\"color: #0000ff;\">{<\/span><br \/>\n<span style=\"color: #0000ff;\">cell = row.CreateCell(j, NPOI.SS.UserModel.CellType.String);<\/span><br \/>\n<span style=\"color: #0000ff;\">if (!(dt.Rows[i][column_names[j].Key] == DBNull.Value || dt.Rows[i][column_names[j].Key] == null))<\/span><br \/>\n<span style=\"color: #0000ff;\">{<\/span><br \/>\n<span style=\"color: #0000ff;\">cell.SetCellValue(dt.Rows[i][column_names[j].Key].ToString());<\/span><br \/>\n<span style=\"color: #0000ff;\">}<\/span><br \/>\n<span style=\"color: #0000ff;\">}<\/span><br \/>\n<span style=\"color: #0000ff;\">}<\/span><br \/>\n<span style=\"color: #0000ff;\">}<\/span><br \/>\n<span style=\"color: #0000ff;\">book.Write(ms);<\/span><br \/>\n<span style=\"color: #0000ff;\">ms.Flush();<\/span><\/p>\n<p><span style=\"color: #0000ff;\">\/\/ \u8f93\u51fa\u526f\u672c\u7684\u4e8c\u8fdb\u5236\u5b57\u8282\u6d41<\/span><br \/>\n<span style=\"color: #0000ff;\">this.Response.Clear();<\/span><br \/>\n<span style=\"color: #0000ff;\">this.Response.ClearContent();<\/span><br \/>\n<span style=\"color: #0000ff;\">this.Response.ClearHeaders();<\/span><\/p>\n<p><span style=\"color: #0000ff;\">this.Response.ContentType = &#8220;application\/ms-excel&#8221;;<\/span><br \/>\n<span style=\"color: #0000ff;\">this.Response.AppendHeader(&#8220;Content-Disposition&#8221;, &#8220;attachment;filename=&#8221; + this.Server.UrlPathEncode(filename));<\/span><br \/>\n<span style=\"color: #0000ff;\">this.Response.BinaryWrite(ms.ToArray());<\/span><br \/>\n<span style=\"color: #0000ff;\">\/\/ \u5220\u9664\u526f\u672c<\/span><br \/>\n<span style=\"color: #0000ff;\">\/\/this.Response.End();<\/span><br \/>\n<span style=\"color: #0000ff;\">book.Close();<\/span><br \/>\n<span style=\"color: #0000ff;\">}<\/span><br \/>\n<span style=\"color: #0000ff;\">catch (Exception)<\/span><br \/>\n<span style=\"color: #0000ff;\">{<\/span><br \/>\n<span style=\"color: #0000ff;\">throw;<\/span><br \/>\n<span style=\"color: #0000ff;\">}<\/span><br \/>\n<span style=\"color: #0000ff;\">}<\/span><\/p>\n<p>3.\u8c03\u7528\uff1a<\/p>\n<p><span style=\"color: #ff6600;\">KeyValuePair&lt;string, string&gt;[] column_names = new KeyValuePair&lt;string, string&gt;[2];<\/span><br \/>\n<span style=\"color: #ff6600;\">\/\/\u8bbe\u7f6e\u6570\u636e\u8868\u5217\u540d\u548c\u5bfc\u51fa\u7684excel\u5217\u540d\u5bf9\u5e94\u5173\u7cfb<\/span><br \/>\n<span style=\"color: #ff6600;\">column_names[0] = new KeyValuePair&lt;string, string&gt;(&#8220;id&#8221;,&#8221;\u5e8f\u53f7&#8221;);<\/span><br \/>\n<span style=\"color: #ff6600;\">column_names[1] = new KeyValuePair&lt;string, string&gt;(&#8220;faultnumber&#8221;, &#8220;\u7f3a\u9677\u5355\u53f7&#8221;);<\/span><\/p>\n<p><span style=\"color: #ff6600;\">ToExcelFile( &#8220;123.xls&#8221;, dt4, column_names);<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.\u7528nuget\u547d\u4ee4\u884c\u5b89\u88c5NPOI\u5f15\u7528\uff1a Install-Package NPOI -Version 2.4. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-6785","post","type-post","status-publish","format-standard","hentry","category-coding"],"_links":{"self":[{"href":"http:\/\/www.yeyaorong.cn\/index.php?rest_route=\/wp\/v2\/posts\/6785","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.yeyaorong.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yeyaorong.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yeyaorong.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yeyaorong.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6785"}],"version-history":[{"count":6,"href":"http:\/\/www.yeyaorong.cn\/index.php?rest_route=\/wp\/v2\/posts\/6785\/revisions"}],"predecessor-version":[{"id":6791,"href":"http:\/\/www.yeyaorong.cn\/index.php?rest_route=\/wp\/v2\/posts\/6785\/revisions\/6791"}],"wp:attachment":[{"href":"http:\/\/www.yeyaorong.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6785"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yeyaorong.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6785"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yeyaorong.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6785"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}