コード例 #1
0
        public IContent WritePage(ISource source, IPageData pageData, OutputData outputData)
        {
            //DataSet ds = outputData.Data as DataSet;
            //TkDebug.AssertArgumentNull(ds, "ds", null);
            //DataTable table = ds.Tables[fMetaData.Table.TableName];

            byte[]      midArray = NPOIWrite.ExportExcel(outputData, this);
            string      fileName = fMetaData.Table.TableDesc + ".xls";
            FileContent file     = new FileContent(NetUtil.GetContentType(fileName), fileName, midArray);

            return(new WebFileContent(file));
        }
コード例 #2
0
ファイル: NPOIRead.cs プロジェクト: hkmujj/zllinmitu
        // 模板
        public static byte[] CreateExcelTemplate(Tk5ListMetaData metaData)
        {
            MemoryStream ms = new MemoryStream();

            using (ms)
            {
                HSSFWorkbook         workbook         = new HSSFWorkbook();
                HSSFSheet            sheet            = (HSSFSheet)workbook.CreateSheet(metaData.Table.TableDesc);
                HSSFRow              dataRow          = (HSSFRow)sheet.CreateRow(0);
                int                  index            = 0;
                ExportExcelPageMaker headerConfigData =
                    new ExportExcelPageMaker(ContentFormat.DefaultHead, ContentFormat.DefaultContent)
                {
                    UserBorder = true
                };
                ExportExcelPageMaker contentConfigData =
                    new ExportExcelPageMaker(ContentFormat.DefaultHead, ContentFormat.DefaultContent)
                {
                    UserBorder = false
                };

                foreach (Tk5FieldInfoEx fieldInfo in metaData.Table.TableList)
                {
                    int colWith = NPOIWrite.GetColWidth(fieldInfo);
                    sheet.SetColumnWidth(index, colWith << 8);
                    ICellStyle styleContent = NPOIWrite.BorderAndFontSetting(workbook,
                                                                             contentConfigData, fieldInfo, NPOIWrite.Model.Content);
                    HSSFDataValidation dataValidate = CreateDataValidation(index, fieldInfo, styleContent, workbook);
                    sheet.SetDefaultColumnStyle(index, styleContent);
                    if (dataValidate != null)
                    {
                        ((HSSFSheet)sheet).AddValidationData(dataValidate);
                    }

                    ICell      cell        = dataRow.CreateCell(index);
                    ICellStyle styleHeader = NPOIWrite.BorderAndFontSetting(workbook,
                                                                            headerConfigData, fieldInfo, NPOIWrite.Model.Header);
                    cell.SetCellValue(fieldInfo.DisplayName);
                    cell.CellStyle = styleHeader;
                    index++;
                }
                //string strFileName = @"D:\EmportTemplateTest.xls";
                //using (FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write))
                //{
                //    workbook.Write(fs);
                //}

                workbook.Write(ms);
                ms.Flush();
                return(ms.ToArray());
            }
        }