C# 使我的SQLite打印方法动态

C# 使我的SQLite打印方法动态,c#,sqlite,csv,printing,C#,Sqlite,Csv,Printing,我正在使用SQLite为我的应用程序存储数据。 该应用程序有一个UI,用于从SQLite数据库加载数据,以便将数据逐表显示给用户。基本上,用户可以单击左键或右键,逐个查看其他表 用户还可以单击一个按钮,该按钮将显示数据的打印预览,并让他们打印数据。我有这个工作,但我有一些问题,设计一个动态的方式来显示任何表格打印预览屏幕上完美。我担心的是,如果某些列标题太长,基本上如何计算每个列的大小等。我应该循环查找整个列中任何文本的最大字符大小,然后将列宽设置为略宽于此,还是有更简单的方法 我还将数据表写入

我正在使用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");
        }

    }
}
/---------------------------单击打印按钮时调用的代码----------------------------
//保存行数据
List myList=新列表();
如果(ReportPage==1)
{
int rowCount=MyTestTable.rowCount;
对于(int i=0;i列)
{
columns=array.Length;
}
}
//添加列。
对于(int i=0;i=17)
{
页面=新页面(报告);
page.setscape();
amountRowsPerPageSoFar=0;
罗维=40;
}
}
捕获(例外情况除外)
{
}
}
RT.ViewPDF(报告,“TestReport.pdf”);
}
}
}