c# Aspose.Cells 經過Excel模板生產excel數據再打印

多的不說,咱們先來利用Northwind作兩個小demo。先說說Aspose.Cells的模板語法:sql

  1. &=DataSource.Field,&=[DataSource].[Field]是對DataTable和幾何類型的引用,將會從當前行開始豎直向下生成多行數據。
  2. &=$data:是對變量或數組的引用。數組存在skiphorizontal等屬性,具體參見
  3. &=&=動態公式計算;{r}當前行,{c}當前列,{-n},{n}當前行或列的偏移量前n或後n。
  4. &==是動態計算,如excel,if等語句。(if(logic_test,true_value,false_value))

還有其餘更爲複雜的彙總計算的表達式,這裏也不在這節多講,有興趣的朋友,能夠去官網看看。咱們先來個簡單的例子,光說,老是不行的,仍是要代碼實踐才能說明一切:數組

代碼以下:.net

App_Code.SqlDbHelper help = new App_Code.SqlDbHelper();
var s = Aspose.Cells.CellsHelper.GetVersion();
var sql = "select WL_TZ,WL_CL,RPCJH_SL,WL_MC,RPCJH_JHKGRQ,RPCJH_JHID,RPCJH_JHID as RPCJH_JHID1,RPCJH_GXID,U_RPCJH_KH,(select rtrim((cast(GYLX_GXID as char(2)))+' '+ltrim(GYLX_GXMC)) from GYLX where GYLX_GYLXID=wo.WO_GYLXID and GYLX_GXID=RPCJH_GXID) as gxmc from RPCJH left join wl on RPCJH_WLID=WL_WLID left join wo on wo.WO_WOID=RPCJH_WOID where RPCJH_JHID='JHA1909152' and RPCJH_BZGXID!='888' ";
var dt = help.ExecuteDataTable(sql);
dt.TableName = "Customers";
Workbook workBook = new Workbook(@"E:\KaBoProject\GTDB\GTDB\bin\Debug\機加做業計劃卡.xls");
WorkbookDesigner designer = new WorkbookDesigner(workBook);
//數據源
designer.SetDataSource(dt);
//報表單位
designer.SetDataSource("RPCJH_JHID", dt.Rows[0]["RPCJH_JHID"].ToString().Trim());
designer.SetDataSource("U_RPCJH_KH", dt.Rows[0]["U_RPCJH_KH"].ToString().Trim());
designer.SetDataSource("RPCJH_JHID1", "計劃編號:" + dt.Rows[0]["RPCJH_JHID1"].ToString().Trim() + " 領卡人:");
designer.SetDataSource("RPCJH_JHKGRQ", "派工日期:" +Convert.ToDateTime(dt.Rows[0]["RPCJH_JHKGRQ"].ToString().Trim()).ToShortDateString());
designer.SetDataSource("WL_MC", "名稱:" + dt.Rows[0]["WL_MC"].ToString().Trim());
designer.SetDataSource("WL_TZ", dt.Rows[0]["WL_TZ"].ToString().Trim());
designer.SetDataSource("WL_CL", dt.Rows[0]["WL_CL"].ToString().Trim());
designer.SetDataSource("RPCJH_SL", dt.Rows[0]["RPCJH_SL"].ToString().Trim());

designer.Process();
workBook.Save(@"E:\KaBoProject\GTDB\GTDB\bin\Debug\2.xls", SaveFormat.Xlsx);

designer = null;excel

生成的excel以下component

打印代碼以下:orm

Workbook workbook = new Workbook(@"E:\KaBoProject\GTDB\GTDB\bin\Debug\2.xls");
           
            //Get the worksheet to be printed
Worksheet worksheet = workbook.Worksheets[0];//獲取該Excel文檔的第一個工做表
            //PageSetup pageSetup = worksheet.PageSetup;
            //pageSetup.Orientation = PageOrientationType.Landscape;
            //pageSetup.LeftMargin = 0;
            //pageSetup.RightMargin = 0.1;          
            //pageSetup.BottomMargin = 0.3;
            //pageSetup.PrintArea = "A2:J29";
            //Apply different Image / Print options.
            Aspose.Cells.Rendering.ImageOrPrintOptions options = new Aspose.Cells.Rendering.ImageOrPrintOptions();blog

            //Set the Printing page property
            //options.PrintingPage = PrintingPageType.IgnoreStyle;
            //Render the worksheet
            SheetRender sr = new SheetRender(worksheet, options);ip

            System.Drawing.Printing.PrinterSettings printSettings = new System.Drawing.Printing.PrinterSettings();
            string strPrinterName = printSettings.PrinterName;文檔

            //send to printer
            //System.Drawing.Image map = sr.ToImage(0);get

            sr.ToPrinter(strPrinterName);

相關文章
相關標籤/搜索