Example #1
0
        public void BuildReport(Window owner)
        {
            if (string.IsNullOrEmpty(PrinterName) && string.IsNullOrWhiteSpace(PrinterName))
            {
                MessageBox.Show(owner, "Please select your printer name.", "Atention", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }

            try
            {
                string dataDir      = System.IO.Directory.GetCurrentDirectory();
                string fullFileName = dataDir + @"\templates\";

                Workbook workbook = new Workbook();
                workbook.LoadFromFile(fullFileName + "Template.xlsx");

                //Edit Text
                Worksheet sheet    = workbook.Worksheets[0];
                DataTable data     = GetData();
                ExcelFont fontBold = workbook.CreateFont();

                sheet.PageSetup.Orientation = PageOrientationType.Landscape;
                fontBold.IsBold             = true;

                var headers = data.AsEnumerable()
                              .Select(s => s.Field <string>("Name"))
                              .ToArray();

                var cellsData = data.AsEnumerable()
                                .Select(s => s.Field <int>("Type"))
                                .ToArray();

                int columnsRange = 3 + headers.Length;

                sheet.InsertArray(headers, 16, 3, false);
                sheet.InsertArray(cellsData, 17, 3, false);

                CellRange range = sheet.Range[16, 3, 16, columnsRange];

                range.Style.Rotation    = 60;
                range.Style.Font.IsBold = true;
                range.AutoFitRows();
                range.ColumnWidth = 4;

                sheet.Range[16, 3, 17, columnsRange].BorderInside(LineStyleType.Thin, Color.Black);
                sheet.Range[16, 3, 17, columnsRange].BorderAround(LineStyleType.Thin, Color.Black);

                workbook.SaveToFile(fullFileName + "report.xlsx", FileFormat.Version2016);
                MessageBox.Show(owner, "Report has been created successfully.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                PrintFile(owner, fullFileName + "report.xlsx");
            }
            catch (Exception ex)
            {
                MessageBox.Show(owner, string.Format("An error has occurred while creating document: {0}", ex.Message), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Example #2
0
        public void CreateFile(Window owner)
        {
            try
            {
                Workbook  workbook = new Workbook();
                Worksheet sheet    = workbook.Worksheets[0];
                DataTable data     = GetData();
                ExcelFont fontBold = workbook.CreateFont();

                sheet.PageSetup.Orientation = PageOrientationType.Landscape;
                fontBold.IsBold             = true;

                var headers = data.AsEnumerable()
                              .Select(s => s.Field <string>("Name"))
                              .ToArray();

                var cellsData = data.AsEnumerable()
                                .Select(s => s.Field <int>("Type"))
                                .ToArray();

                int columnsRange = 2 + headers.Length - 1;

                sheet.InsertArray(headers, 2, 2, false);
                sheet.InsertArray(cellsData, 3, 2, false);

                CellRange range = sheet.Range[2, 2, 2, columnsRange];

                range.Style.Rotation    = 60;
                range.Style.Font.IsBold = true;
                range.AutoFitRows();
                range.ColumnWidth = 4;

                sheet.Range[2, 2, 3, columnsRange].BorderInside(LineStyleType.Thin, Color.Black);
                sheet.Range[2, 2, 3, columnsRange].BorderAround(LineStyleType.Thin, Color.Black);

                workbook.SaveToFile(FileName, GetFileFormat());
                System.Windows.MessageBox.Show(owner, "File has been created successfully.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);

                workbook.SaveToFile("file.xps", FileFormat.XPS);
                PrintDialog dlg    = new PrintDialog();
                XpsDocument xpsDoc = new XpsDocument(@"file.xps", System.IO.FileAccess.Read);
                dlg.PrintDocument(xpsDoc.GetFixedDocumentSequence().DocumentPaginator, "Document title");
                //PrintFile(owner, "file.xps");
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(owner, string.Format("An error has occurred while creating document: {0}", ex.Message), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Example #3
0
        private bool GetArchContenWriteXlsPrint(int archid)
        {
            DataTable ArchConten = Common.GetPrintConten(archid, ClsPrintConten.PrintContenTable, ClsPrintConten.PrintContenCol, ClsPrintConten.PrintContenPagesn);

            if (ArchConten == null || ArchConten.Rows.Count <= 0)
            {
                string str = "ID号:" + archid + "目录获取失败!";
                WriteLog(str);
                return(false);
            }
            int countpage = Common.GetArchPages(archid);

            try {
                Workbook  work   = new Workbook();
                Worksheet wsheek = null;
                work.LoadFromFile(Path.Combine(Application.StartupPath, "目录.xls"));
                wsheek = work.Worksheets[0];
                string strsn = ClsPrintConten.PrintContenSn;
                //获取起始行和列
                int rowsn = 0;
                int colsn = 0;
                if (strsn.Trim().Length > 1)
                {
                    rowsn = Convert.ToInt32(strsn.Remove(0, 1));
                    colsn = ClsInfo.ToNum(strsn.Substring(0, 1)) + 1;
                }
                int arow = 0;
                int bcol = 0;
                int dz   = 0;
                for (int i = 0; i < ArchConten.Rows.Count; i++)
                {
                    dz = 0;
                    for (int j = 0; j < ArchConten.Columns.Count; j++)
                    {
                        string str = ArchConten.Rows[i][j].ToString();
                        if (j == 4)
                        {
                            str = str.PadLeft(3, '0');
                        }
                        if (j == 1)
                        {
                            string s = str.Substring(0, 1);
                            if (s.Contains("\\") || s.Contains("/"))
                            {
                                str = str.Substring(1, str.Length - 1);
                            }
                            s = str.Substring(str.Length - 1, 1);
                            if (s.Contains("\\") || s.Contains("/"))
                            {
                                str = str.Substring(0, str.Length - 1);
                            }
                        }
                        if (rowsn > 0)
                        {
                            wsheek.Range[rowsn + i, colsn].Text = (i + 1).ToString();
                        }
                        if (ClsPrintConten.printContenXls.Count > 0)
                        {
                            if (dz < ClsPrintConten.printContenXls.Count)
                            {
                                arow = Convert.ToInt32(ClsPrintConten.printContenXls[dz].Remove(0, 1));
                                bcol = ClsInfo.ToNum(ClsPrintConten.printContenXls[dz].Substring(0, 1)) + 1;
                            }
                            if (dz < ClsPrintConten.PrintContenPage.Count && ClsPrintConten.PrintContenPage[dz] == "True")
                            {
                                if (ClsPrintConten.PrintContenPageMode == 2)
                                {
                                    int p  = Convert.ToInt32(str);
                                    int p1 = 0;
                                    try {
                                        if (i != ArchConten.Rows.Count - 1)
                                        {
                                            p1 = Convert.ToInt32(ArchConten.Rows[i + 1][j].ToString());
                                            if (p == p1)
                                            {
                                                str = p + "-" + p1;
                                            }
                                            else
                                            {
                                                str = p + "-" + (p1 - 1);
                                            }
                                        }
                                        else
                                        {
                                            p1 = countpage;
                                            if (p == p1)
                                            {
                                                str = p + "-" + p1;
                                            }
                                            else
                                            {
                                                str = p + "-" + (p1 - 1);
                                            }
                                        }
                                    } catch {
                                        string str1 = "ID号:" + archid + "目录页码不正确";
                                        WriteLog(str1);
                                        return(false);
                                    }
                                }
                                else
                                {
                                    if (i == ArchConten.Rows.Count - 1)
                                    {
                                        //int p = Convert.ToInt32(str);
                                        int p1 = countpage;
                                        str = str.PadLeft(3, '0') + "-" + p1.ToString().PadLeft(3, '0');
                                    }
                                }
                                wsheek.Range[arow + i, bcol].Text = str;
                            }
                            //else if (dz < ClsPrintConten.PrintContenDz.Count && ClsPrintConten.PrintContenDz[dz] == "True")
                            //    wsheek.Range[arow + i, bcol].Text = str;
                            //else if (arow == i && j == bcol) {
                            //    wsheek.Range[arow + i, bcol].Text = str;
                            //}
                            else if (ClsPrintConten.PrintContenDz[dz] == "False")
                            {
                                string s = ClsPrintConten.printContenXls[dz].ToString();
                                if (str.Trim().Length > 0)
                                {
                                    wsheek.Range[s].Text = str.PadLeft(4, '0');
                                }
                            }
                            else if (ClsPrintConten.PrintContenDz[dz] == "True")
                            {
                                wsheek.Range[arow + i, bcol].Text = str;
                            }
                        }
                        dz += 1;
                    }
                }
                rowsn = wsheek.LastRow;
                string    fontname = wsheek.Rows[6].Cells[3].Style.Font.FontName;
                double    fontsize = wsheek.Rows[6].Cells[3].Style.Font.Size;
                CellRange range    = wsheek.Range["A6" + ":H" + rowsn];
                range.BorderInside(LineStyleType.Thin, ExcelColors.Black);
                range.BorderAround(LineStyleType.Thin, ExcelColors.Black);
                range.Style.Font.Size     = fontsize;
                range.Style.Font.FontName = fontname;
                range.HorizontalAlignment = HorizontalAlignType.Center;
                range.VerticalAlignment   = VerticalAlignType.Center;
                range.Style.WrapText      = true;
                range.AutoFitRows();
                for (int i = 6; i < rowsn; i++)
                {
                    double row = wsheek.Rows[i].RowHeight;
                    if (row < 50)
                    {
                        wsheek.Rows[i].SetRowHeight(50, true);
                    }
                    wsheek.Range["E" + (i + 1) + ":F" + (i + 1)].Merge();
                }
                wsheek.PageSetup.PrintTitleRows = "$1:$5";
                work.PrintDocument.Print();
                return(true);
            } catch (Exception e) {
                string str = "ID号:" + archid + ":" + e;
                WriteLog(str);
                return(false);
            }
        }