使我的SQLite打印方法动态

时间:2015-04-22 09:59:12

标签: c# sqlite csv printing

我正在使用SQLite来存储我的应用程序的数据。 该应用程序具有一个UI,用于从SQLite数据库加载数据,以逐个表的形式将其显示给用户。基本上用户可以向左或向右单击并逐个查看其他表。

用户还可以单击一个按钮,该按钮将显示数据的打印预览并让它们打印出来。我有这个工作,但我有一些问题设计一个动态的方式来完美地显示打印预览屏幕上的任何表。我担心的是,如果某些列标题太长等,基本上如何计算每列的大小等。我应该遍历并查找整列中任何文本的最大字符大小,然后将列宽设置为比或者有更简单的方法吗?

我还将数据表写入逗号分隔的csv文件中,因此如果有人知道这样的解决方案,那么它可能是使用现有解决方案从csv文件打印的更好的替代方案,请建议它!

无论如何这里是现有的代码:

// ------------------------ code that gets called when the print button is clicked ----------------------------

// holds the row data
List<string[]> myList = new List<string[]>();

if(ReportPage == 1)
{
    int rowCount = MyTestTable.RowCount;
    for(int i = 0; i <rowCount; i++)
    {
         MyTestTable.SelectedRowIndex = i;
         var row1 = MyTestTable.GetSelectedRow();
         var cols1 = row1.ItemArray;

        string Col1 = cols1[row1.FindIndexOfColumn("Col1")].ToString();
        string Col2 = cols1[row1.FindIndexOfColumn("Col2")].ToString();
        string Col3 = cols1[row1.FindIndexOfColumn("Col3")].ToString();
        string Col4 = cols1[row1.FindIndexOfColumn("Col4")].ToString();
        string Col5 = cols1[row1.FindIndexOfColumn("Col5")].ToString();
        string Col6 = cols1[row1.FindIndexOfColumn("Col6")].ToString();
        string Col7 = cols1[row1.FindIndexOfColumn("Col7")].ToString();
        myList.Add(new string[] { Col1, Col2, Col3, Col4, Col5, Col6, Col7 });
    }

string[] cols = new string[7];
cols[0] = "Col1";
cols[1] = "Col2";
cols[2] = "Col3";
cols[3] = "Col4";
cols[4] = "Col5";
cols[5] = "Col6";
cols[6] = "Col7";

PrintUtility.SetUpDocument("TEST", cols, myList);


}
PrintUtility.TestNewReport();






// ---------------------- plugin code that gets called from the application 

namespace PrintUtility
{

     public class PrintUtility : UserComponentBase
    {
        public PrintDocument document;
        public DataGridView dataGridView;

        public PrintUtility()
        {
            document = new PrintDocument();
            dataGridView = new DataGridView();
        }



        public void SetUpDocument(string title, string[] cols,  List<string[]> rows)
        {
            document = new PrintDocument();
            dataGridView = new DataGridView();
            document.DocumentName = title;
            document.DefaultPageSettings.Landscape = true;
            document.PrintPage += PrintPage;

            DataGridView dataGrid = new DataGridView();
            dataGrid.ColumnCount = cols.Length;
            for (int i = 0; i < cols.Length; i++ )
            {
                dataGrid.Columns[i].Name = cols[i];
            }

            foreach(string[] row in rows)
            {
                dataGrid.Rows.Add(row);
            }

            this.dataGridView = dataGrid;
            document.DocumentName = title;
            document.PrintPage += PrintPage;
        }


        public PrintDocument GetDocument()
        {
            return this.document;
        }

        private DataTable ConvertListToDataTable(List<string[]> list)
        {
            // New table.
            DataTable table = new DataTable();

            // Get max columns.
            int columns = 0;
            foreach (var array in list)
            {
                if (array.Length > columns)
                {
                    columns = array.Length;
                }
            }

            // Add columns.
            for (int i = 0; i < columns; i++)
            {
                table.Columns.Add();
            }

            // Add rows.
            foreach (var array in list)
            {
                table.Rows.Add(array);
            }

            return table;
        }

        public void TestNewReport()
        {
            Report report = new Report(new PdfFormatter());
            FontDef fd = new FontDef(report, "Helvetica");
            FontProp fp = new FontPropMM(fd, 4);
            FontProp fp_Title = new FontPropMM(fd, 6);
            FontProp fp_Word = new FontPropMM(fd, 3);
            fp_Title.bBold = true;

            List<string> columns = new List<string>();
            foreach (DataGridViewColumn column in dataGridView.Columns)
            {
                columns.Add(column.Name.ToString());
            }

            List<List<string>> rows = new List<List<string>>();
            foreach (DataGridViewRow row in dataGridView.Rows)
            {
                List<string> rowSingle = new List<string>();
                foreach (DataGridViewCell cell in row.Cells)
                {
                    try
                    {
                        rowSingle.Add(cell.Value.ToString());
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
                }
                rows.Add(rowSingle);
            }

            // AUDIT TABLE ( This way of doing things is not dynamic )
            Page page = new Page(report);
            page.SetLandscape();
            int x = 10;
            int y = 40;
            int pastLength = 0;
            foreach(string col in columns)
            {
                x += ((pastLength * 14) + 31);
                page.Add(x, y, new RepString(fp_Title, col));
                pastLength = col.Length;

            }
            page.Add(0, 52, new RepString(fp_Title, "_________________________________________________________________"));

            /* Dynamic way starting
            int rowX = 10;
            int rowY = 105;
            foreach (List<string> row in rows)
            {
                int pastLength2 = 0;
                foreach (string rowItem in row)
                {
                    page.Add(rowX, rowY, new RepString(fp_Word, rowItem));
                    rowX += ((pastLength * 14) + 31);
                    pastLength2 = rowItem.Length;
                }
                rowY += 30;
            }
             */

            fp_Title.rSizeMM = 8;
            int amountRowsPerPageSoFar = 0;
            int rowY = 80;

            foreach (List<string> row in rows)
            {
                try
                {
                    string iDItem = row[0];
                    page.Add(40, rowY, new RepString(fp_Word, iDItem));

                    string typeItem = row[1];
                    page.Add(120, rowY, new RepString(fp_Word, typeItem));

                    string descriptionItem = row[2];
                    page.Add(190, rowY, new RepString(fp_Word, descriptionItem));

                    string timestampItem = row[3];
                    page.Add(375, rowY, new RepString(fp_Word, timestampItem));

                    string userItem = row[4];
                    page.Add(555, rowY, new RepString(fp_Word, userItem));

                    string stationItem = row[5];
                    page.Add(655, rowY, new RepString(fp_Word, stationItem));

                    string activeItem = row[6];
                    page.Add(775, rowY, new RepString(fp_Word, activeItem));
                    amountRowsPerPageSoFar++;

                    rowY += 30;
                    if (amountRowsPerPageSoFar >= 17)
                    {
                        page = new Page(report);
                        page.SetLandscape();
                        amountRowsPerPageSoFar = 0;
                        rowY = 40;
                    }

                }
                catch (Exception ex)
                {

                }
            }

            RT.ViewPDF(report, "TestReport.pdf");
        }

    }
}

0 个答案:

没有答案