NPOI 写download Excel 功能

1.后台代码

        public Object DownloadToExcel(JObject searchCriteria, JObject validCriteria)
        {
            Dictionary<string, object> returnData = (Dictionary<string, object>)this.InitData(searchCriteria); 
            Object dObject = this.InitData(searchCriteria);
            DataTable dt = ((DataSet)returnData["data"]).Tables[0];
            String template = AppDomain.CurrentDomain.BaseDirectory + System.Configuration.ConfigurationSettings.AppSettings["ReportsExcelPath"] + "Template\test123.xlsx";
            IWorkbook iworkbook;
            using (FileStream file = new FileStream(template, FileMode.Open, FileAccess.Read))
            {
                iworkbook = new XSSFWorkbook(file);
                file.Close();
            }
            var parameters = JsonConvert.DeserializeObject<Dictionary<string, object>>(searchCriteria.ToString());
            ISheet sheet1 = iworkbook.GetSheetAt(0);
            ICell TitleMsg = sheet1.GetRow(0).GetCell(0);
           
            IRow sourceRow = sheet1.GetRow(6);
            IRow headsourceRow = sheet1.GetRow(4);
            int i = 6, j;
            DateTime currentTime = DateTime.Now;
            TitleMsg.SetCellValue(string.Format("on:{0},{1} {2} {3} {4}", currentTime.DayOfWeek.ToString(), currentTime.Day, strMonth[currentTime.Month], currentTime.Year.ToString(), currentTime.TimeOfDay.ToString().Substring(0, 8)));
            //int headIndex = 4;
            IRow headerRow = sheet1.GetRow(3);
            ICell Region = sheet1.GetRow(4).GetCell(2);
            Region.SetCellValue(parameters["RegionText"].ToString());
            foreach (DataRow row in dt.Rows)
            {

                IRow irow = sheet1.CreateRow(i);

                j = 0;
                foreach (DataColumn column in dt.Columns)
                {


                    String value = row[dt.Columns[j].ColumnName].ToString();
                    ICell icell = irow.CreateCell(j);
                    icell.CellStyle = sourceRow.Cells[0].CellStyle;
                    icell.SetCellValue(value);
                    j++;
                }
                i++;
            }

            String filename = "newfile" + DateTime.Now.ToString("yyyyMMMdd_HHmmss") + ".xlsx";
            String fullpath = AppDomain.CurrentDomain.BaseDirectory + "Document\Reports\Output\" + filename;

            FileStream files = new FileStream(fullpath, FileMode.Create);
            iworkbook.Write(files);
            files.Close();
            returnData.Add("filename", "Document\Reports\Output\" + filename);
            if (File.Exists(fullpath))
            {
                return returnData;
            }
            else
            {
                return null;
            }

        }

2.前台使用Angular.js获取生成的表格并抛出浏览器提示:

            vm.downloadToExcel = function () {
                var promise = webapi.post("DownloadService", "DownloadToExcel", { "searchCriteria": vm.searchParamters, "validCriteria": vm.criteria });
                promise.then(
                    function (data) {
                        vm.filePath = data.data.filename;
                        if (vm.filePath != "") {
                            window.location.href = vm.filePath;
                        }
                    },
                    function (data) {
                        logger.error("error");
                    });
            };

 3.C#代码调整Excel的格式(待更新):

wrap text 某列: 

IWorkbook iworkbook;
ISheet sheet1 = iworkbook.GetSheetAt(0);
sheet1.AutoSizeColumn(2);  // 表示调整第三列的格式by wrap text
原文地址:https://www.cnblogs.com/Aaron-Lee/p/7149765.html