.Net Core Excel 템플릿 읽기 Excel 파일 내보내기

1452 단어 .NETCore
            var exportTemplatePath = "./Template/cxtemplate.xlsx";
            var newName =  $"xxx -{System.DateTime.Today.ToString("yyyy-MM-dd")}.xlsx";
            var exprotPath = "wwwroot/Excel/" + newName;
            if (File.Exists(exprotPath))
            {
                File.Delete(exprotPath);
            }
            File.Copy(exportTemplatePath, exprotPath);
            FileStream file = new FileStream(exprotPath, FileMode.Open, FileAccess.Read);
            var  workbook = new XSSFWorkbook(file);
            var sheet = workbook.GetSheet("Sheet1");
            //List> dict = GetAllData();
            //string json = JsonConvert.SerializeObject(dict);
            //System.Data.DataTable table = JsonToObj(json, "");
            //  DataTable  
            int rowIndex = sheet.LastRowNum + 1;

            foreach (DataRow row in table.Rows)
            {
                    XSSFRow dataRow = (XSSFRow)sheet.CreateRow(rowIndex);
                    foreach (DataColumn column in table.Columns)
                        dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
                    ++rowIndex;
            }

            using (var stream = new FileStream(exprotPath, FileMode.Create, FileAccess.Write))
            {
                workbook.Write(stream);
            }

       

좋은 웹페이지 즐겨찾기