在C#中根据Excel文件画FastReport .NET报表模板

 用NPOI改写了读取EXCEL的部分,然后加入到FP的源码( http://www.commuch.com )中去以插件的形式存在。如图



    public void LoadReport(string fileName)
        {

            WorkBook book = new WorkBook(fileName);
            List<string> sheetNames = new List<string>();
            for (int i = 0; i < book.SheetCount; i++) {
                sheetNames.Add(book[i].Name);
            }
            string LeadOutPath = string.Empty;
            List<string> SelectedNames = new List<string>();
            using (FormSheetSelect frm = new FormSheetSelect().Start(sheetNames)) {
                if (frm.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
                    return ;
                LeadOutPath = frm.LeadOutPath;
                SelectedNames.AddRange(frm.SelectedItems);
            }


            for (int i = 0; i < book.SheetCount; i++) {
                if (!SelectedNames.Contains(book[i].Name))
                    continue;
                IWorkSheet sheet = book[i];
                if (sheet.RowCount < 1)
                    continue;

                System.IO.StringWriter mem = new System.IO.StringWriter();
                System.Xml.XmlTextWriter sw = new System.Xml.XmlTextWriter(mem);
                sw.Formatting = System.Xml.Formatting.Indented;
                sw.WriteStartElement("ReportPage");
                sw.WriteAttributeString("Name", "Page1");
                #region 处理纸张

                System.Drawing.Size paperSize = new System.Drawing.Size();
                sw.WriteAttributeString("RawPaperSize", sheet.PageSettings.PaperSize.RawKind.ToString());
                paperSize = new System.Drawing.Size(Inch2Pix(sheet.PageSettings.PaperSize.Width / 100, true), Inch2Pix(sheet.PageSettings.PaperSize.Height / 100, false));
                if (sheet.PageSettings.Landscape) {
                    sw.WriteAttributeString("Landscape", "true");
                    sw.WriteAttributeString("PaperWidth", GetPaperSize(sheet.PageSettings.PaperSize).Height.ToString());
                    sw.WriteAttributeString("PaperHeight", GetPaperSize(sheet.PageSettings.PaperSize).Width.ToString());
                }
                else {
                    sw.WriteAttributeString("PaperWidth", GetPaperSize(sheet.PageSettings.PaperSize).Width.ToString());
                    sw.WriteAttributeString("PaperHeight", GetPaperSize(sheet.PageSettings.PaperSize).Height.ToString());

                }
                //bound
                sw.WriteAttributeString("LeftMargin", Inch2mm(sheet.PageSettings.Margins.Left / 100d).ToString());
                sw.WriteAttributeString("TopMargin", Inch2mm(sheet.PageSettings.Margins.Top / 100d).ToString());
                sw.WriteAttributeString("RightMargin", Inch2mm(sheet.PageSettings.Margins.Right / 100d).ToString());
                sw.WriteAttributeString("BottomMargin", Inch2mm(sheet.PageSettings.Margins.Bottom / 100d).ToString());

                #endregion

                #region 写标题
                sw.WriteStartElement("ReportTitleBand");
                sw.WriteAttributeString("Name", "ReportTitle1");
                if (sheet.PageSettings.Landscape) {

                    sw.WriteAttributeString("Height", (paperSize.Width - (sheet.PageSettings.Margins.Left + sheet.PageSettings.Margins.Right)).ToString());
                    sw.WriteAttributeString("Width", (paperSize.Height - (sheet.PageSettings.Margins.Top + sheet.PageSettings.Margins.Bottom)).ToString());
                }
                else {
                    sw.WriteAttributeString("Width", (paperSize.Width - (sheet.PageSettings.Margins.Left + sheet.PageSettings.Margins.Right)).ToString());
                    sw.WriteAttributeString("Height", (paperSize.Height - (sheet.PageSettings.Margins.Top + sheet.PageSettings.Margins.Bottom)).ToString());

                }
                sw.WriteStartElement("TableObject");
                sw.WriteAttributeString("Name", "Table2");
                if (sheet.PageSettings.Landscape) {

                    sw.WriteAttributeString("Height", (paperSize.Width - (sheet.PageSettings.Margins.Left + sheet.PageSettings.Margins.Right)).ToString());
                    sw.WriteAttributeString("Width", (paperSize.Height - (sheet.PageSettings.Margins.Top + sheet.PageSettings.Margins.Bottom)).ToString());
                }
                else {
                    sw.WriteAttributeString("Width", (paperSize.Width - (sheet.PageSettings.Margins.Left + sheet.PageSettings.Margins.Right)).ToString());
                    sw.WriteAttributeString("Height", (paperSize.Height - (sheet.PageSettings.Margins.Top + sheet.PageSettings.Margins.Bottom)).ToString());

                }
                //sw.WriteAttributeString("ManualBuildEvent", "Table2_ManualBuild");
                sw.WriteAttributeString("AfterDataEvent", "Table2_AfterData");

                #endregion
                double pi = 1;
                try {
                    //指定了缩放条件则缩放
                    pi = (int)sheet.Scale / 100d;
                }
                catch {
                    pi = 1;
                }
                //认为表单是规则的二维表
                IRow firstRow = sheet.FirstRow;
                int cellCount = firstRow.CellCount;//强制以标题占用的单元格数目为准
                if (firstRow.FirstCell != null) {
                    System.Drawing.Point firtCellPoint = firstRow.FirstCell.MergeCellsCount;
                    if (firtCellPoint.Y > 0) {
                        cellCount = firtCellPoint.Y;
                    }
                }
                for (int c = firstRow.FirstCellIndex; c < cellCount + firstRow.FirstCellIndex; c++) {
                    sw.WriteStartElement("TableColumn");
                    sw.WriteAttributeString("Name", "Column" + c.ToString());
                    sw.WriteAttributeString("Width", (pi * sheet.GetColumnWidth(c)).ToString());
                    sw.WriteEndElement();
                }
                
                    for (int r = sheet.FirstRowIndex; r < sheet.RowCount + sheet.FirstRowIndex; r++) {
                    firstRow = sheet.Rows(r);
                    if (firstRow == null)
                        continue;
                    sw.WriteStartElement("TableRow");
                    sw.WriteAttributeString("Name", "Row" + r.ToString());
                    sw.WriteAttributeString("Height", Point2Pix(pi * firstRow.Height).ToString());


                    for (int c = sheet.Rows(r).FirstCellIndex; c <= cellCount + firstRow.FirstCellIndex; c++) {

                        ICell cell = firstRow[c];
                        if (cell == null)
                            continue;
                        sw.WriteStartElement("TableCell");
                        sw.WriteAttributeString("Name", "Cell" + (c * 1000 + r).ToString());
                        if (cell.Text != null) {
                            sw.WriteStartAttribute("Text");
                            double d;
                            if (double.TryParse(cell.Text.ToString(), out d) && d == 0) {
                                sw.WriteValue("");
                            }
                            else sw.WriteValue(cell.Text);
                            sw.WriteEndAttribute();
                        }
                        if (cell.TextOrientation)
                            sw.WriteAttributeString("TextOrientation", "True");
                        sw.WriteAttributeString("HideZeros", "true");
                        sw.WriteStartAttribute("HorzAlign");
                        sw.WriteValue(cell.HorizontalAlignment.ToString());

                        sw.WriteEndAttribute();
                        sw.WriteAttributeString("VertAlign", cell.VerticalAlignment.ToString());
                        sw.WriteAttributeString("Font", cell.Font(pi));
                        System.Drawing.Point MergeCellsCount = cell.MergeCellsCount;
                        if (MergeCellsCount.Y > 1)
                            sw.WriteAttributeString("ColSpan", MergeCellsCount.Y.ToString());
                        if (MergeCellsCount.X > 1)
                            sw.WriteAttributeString("RowSpan", MergeCellsCount.X.ToString());

                        Type dataType = cell.DataType;
                        if (dataType.Equals(typeof(double))) {
                            sw.WriteAttributeString("Format", "Number");
                            sw.WriteAttributeString("Format.UseLocale", "false");
                            sw.WriteAttributeString("Format.DecimalDigits", "2");
                            sw.WriteAttributeString("Format.DecimalSeparator", ".");
                            sw.WriteAttributeString("Format.GroupSeparator", "");
                            sw.WriteAttributeString("Format.NegativePattern", "1");
                        }
                        else
                            if (dataType.Equals(typeof(int))) {
                                sw.WriteAttributeString("Format", "Number");
                                sw.WriteAttributeString("Format.UseLocale", "false");
                                sw.WriteAttributeString("Format.DecimalDigits", "0");
                                sw.WriteAttributeString("Format.DecimalSeparator", ".");
                                sw.WriteAttributeString("Format.GroupSeparator", "");
                                sw.WriteAttributeString("Format.NegativePattern", "1");
                                break;
                            }



                        string line = string.Format("{0}{1}{2}{3}",
                                    cell.HasBorder(0) ? ",Left" : "",
                                    (MergeCellsCount.Y > 1) ? (firstRow[c + MergeCellsCount.Y - 1].HasBorder(1) ? ",Right" : "") : (cell.HasBorder(1) ? ",Right" : ""),
                                    cell.HasBorder(2) ? ",Top" : "",
                                    (MergeCellsCount.X > 1) ? (sheet[r + MergeCellsCount.X - 1, c].HasBorder(3) ? ",Bottom" : "") : (cell.HasBorder(3) ? ",Bottom" : "")

                                  );


                        if (line.Length > 0)
                            sw.WriteAttributeString("Border.Lines", line.Substring(1));


                        sw.WriteEndElement();



                    }
                    sw.WriteEndElement();
                }

                sw.WriteEndElement();
                sw.WriteEndElement();
                sw.WriteEndElement();

                mem.Flush();
                if (string.IsNullOrEmpty(LeadOutPath)) {
                     Report.Load(
                         new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(
                         template.Replace("$#report#{1}quot;, mem.ToString().Replace("
", "
")))));
                }
                else
                {
                    if (!System.IO.Directory.Exists(System.IO.Path.Combine(LeadOutPath,System.IO.Path.GetFileNameWithoutExtension( book.FullName))))
                        System.IO.Directory.CreateDirectory(System.IO.Path.Combine(LeadOutPath, System.IO.Path.GetFileNameWithoutExtension(book.FullName)));

                    System.IO.File.WriteAllText(
                                       System.IO.Path.Combine(LeadOutPath, System.IO.Path.GetFileNameWithoutExtension(book.FullName) + "\\" + sheet.Name + ".frx"),
                                       template.Replace("$#report#{1}quot;, mem.ToString().Replace("
", "
")), System.Text.Encoding.UTF8);
                }
                sw.Close();
                mem.Close();
                if (string.IsNullOrEmpty(LeadOutPath))
                    break;//非导出模式只处理第一个表单

            }


            book.Close();

            if (!string.IsNullOrEmpty(LeadOutPath)) {
                System.Windows.Forms.MessageBox.Show("ok");

            }
        }




转换的报表几乎与原表一样,总算不再需要FP来画表了,画表永远 是EXCEL最爽。












以下为EXCEL中的效果



  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
### 回答1: FastReport.net文用户手册2019是一本用于指导用户使用FastReport.net报表生成工具的手册。它包含了关于FastReport.net的详细介绍、安装指南、基本功能和高级功能的使用方法等内容。 首先,手册会介绍FastReport.net的基本概念和架构,让用户了解到它是一个基于.NET平台的报表生成库,可以用于在各种.NET应用程序生成丰富、精美的报表。 然后,手册会指导用户如何正确地安装FastReport.net,包括下载安装包、配置环境、注册许可证等步骤。这有助于用户快速地开始使用这个工具。 接下来,手册将详细介绍FastReport.net的各种基本功能,比如报表设计、数据源连接、数据绑定、布局设计、样式设置等。用户可以学习如何创建模板、设计报表、添加数据源,并且对报表进行各种调整和设置。 此外,手册还会介绍FastReport.net的高级功能,比如脚本编写、参数设置、图表绘制、导出和打印等。这些高级功能可以帮助用户根据自己的需求更灵活地生成报表,并且对报表进行进一步的处理和分析。 最后,手册会提供一些实际应用案例和常见问题解答,帮助用户更好地理解和使用FastReport.net。用户可以参考这些案例和解答,解决在实际使用遇到的问题。 总之,FastReport.net文用户手册2019是一本全面的指南,能够帮助用户快速学习和掌握这个报表生成工具,并且在实际应用灵活运用。无论是初学者还是有经验的用户,都可以从受益。 ### 回答2: FastReport.Net是一款功能强大的报表生成工具,该工具提供了强大的报表设计功能和丰富的报表导出选项。FastReport.Net文用户手册2019是FastReport.Net官方为文用户编写的一本指导手册,旨在帮助用户更好地了解和学习使用FastReport.Net。 手册主要涵盖了FastReport.Net的各项功能和特性的详细介绍,包括报表设计、数据源配置、报表元素的添加和编辑、报表样式的修改、报表导出等。手册以简洁明了的方式呈现了每个功能模块的使用方法和注意事项,并附带了丰富的抽样代码和示例,以便用户更好地理解和运用。 通过阅读FastReport.Net文用户手册2019,用户可以系统地学习和了解FastReport.Net的使用方法和技巧,同时也可以通过手册提供的代码示例来实际操作和练习,从而掌握更高级的报表设计和生成技能。手册还提供了常见问题解答和技术支持信息,方便用户在学习过程解决遇到的问题。 对于初学者来说,FastReport.Net文用户手册2019是一份非常实用的学习资料,可以帮助他们快速入门并掌握FastReport.Net的基本使用方法。对于有一定经验的用户来说,手册可以作为快速参考工具,帮助他们解决报表设计和生成的问题和难题。 总之,FastReport.Net文用户手册2019是一本非常有价值的报表生成工具的用户指南,对于FastReport.Net的学习和使用都起到了极大的帮助作用,为用户提供了详尽的功能说明和丰富的代码示例,助力用户在工作更加高效和准确地生成报表。 ### 回答3: FastReport是一款功能强大的报表生成工具,FastReport.Net则是FastReport专为.Net平台开发的版本。FastReport.Net文用户手册2019是针对该工具的用户手册,具体介绍并指导了FastReport.Net的使用。 FastReport.Net文用户手册2019主要包含了工具的安装和配置、报表设计、数据源设置、报表输出等方面的内容。用户可以通过该手册了解到如何在.Net平台上安装和配置FastReport.Net,并全面了解报表设计的各种元素、属性和功能。手册还详细介绍了如何设置和管理数据源,进行数据的筛选、排序和分组等操作。同时,手册还指导用户如何将报表以不同的格式输出,如PDF、Excel、HTML等。 FastReport.Net文用户手册2019的编排方式简洁明了,图文并茂,方便用户理解和操作。手册还提供了大量的例子和示意图,帮助用户更好地理解报表的设计和使用。此外,手册还给出了常见问题的解答和解决方案,帮助用户在使用工具时能够及时解决遇到的问题。 总的来说,FastReport.Net文用户手册2019是FastReport.Net报表生成工具的重要参考资料,它通过清晰的编排和丰富的内容,帮助用户快速掌握和使用FastReport.Net的各种功能,提高报表的生成效率和质量。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值