格式化DataTable

时间:2017-11-28 20:10:09

标签: c# html datatable

我正在尝试格式化我的数据表,以便所有列都正确排列。问题在于,由于每个单元格在字符中包含不同的长度,因此每个单元格的数量多于或少于我希望的数量。有没有简单的方法来格式化dataTable单元格?以下代码是我创建表的代码:

public static void ErrorLog(OdbcConnection connection, string filepath)
    {
        int ID = 0;
        DateTime now = DateTime.Now;

        XDocument theFile = XDocument.Load("C:\\Users\\bob\\Documents\\UpdateW5.xml");

        DataTable theTable = new DataTable("Error Log");
        //Set the report filename
        string filename = @"ErrorLog.html";
        //Check to see if directory exists, if not create it.
        if (!Directory.Exists(filepath)) Directory.CreateDirectory(filepath);
        //Open file for output
        TextWriter webPage = new StreamWriter(filepath + filename, false);

        //Page header
        webPage.WriteLine("<html>");
        webPage.WriteLine("<head>");
        webPage.WriteLine("<title>ErrorLog</title");
        webPage.WriteLine("</head>");

        //Page body
        webPage.WriteLine("<body>");

        //Start an output table
        webPage.WriteLine("<h1>ErrorLog</h1>");
        webPage.WriteLine("<table border=1>");
        webPage.WriteLine("<tr>"); //header row of table
        webPage.WriteLine("<th>ERROR ID</th>");
        webPage.WriteLine("<th>ITEM ID</th>");
        webPage.WriteLine("<th>DATE/TIME</th>");
        webPage.WriteLine("<th>DESCRIPTION</th>");

        foreach (XElement el in theFile.Root.Elements())
        {
            //If the node item is called ADD run the following code
            if (el.Name == "ADD")
            {
                //Check if each attribute has a value
                if (el.Attribute("invent_id") != null && el.Attribute("item_id") != null && el.Attribute("itemsize") != null && el.Attribute("color") != null && el.Attribute("curr_price") != null && el.Attribute("qoh") != null)
                {
                    Item.Item_ID = Convert.ToInt32(el.Attribute("item_id").Value);
                    Item.Invent_id = Convert.ToInt32(el.Attribute("invent_id").Value);
                    Item.Itemsize = el.Attribute("itemsize").Value;
                    Item.Color = el.Attribute("color").Value;
                    Item.Curr_price = Convert.ToDecimal(el.Attribute("curr_price").Value);
                    Item.Qoh = Convert.ToInt32(el.Attribute("qoh").Value);

                    if (Item.Qoh < 1)
                    {
                        webPage.WriteLine("<table border=1>");
                        webPage.WriteLine("<col align = justify>");
                        webPage.WriteLine("<tr>"); //header row of table
                        webPage.WriteLine("<td>" + ID++ + "</td>");
                        webPage.WriteLine("<td>" + now + "</td>");
                        webPage.WriteLine("<td>" + Item.Item_ID + "</td>");
                        webPage.WriteLine("<td> not added: The quantity must be positive</td>");
                    }
                    //If the quantity is above 1, then run the queries
                    else
                    {
                        //Check to make sure the item is not already in the database
                        connection.Open();
                        OdbcCommand Command1 = new OdbcCommand("SELECT * FROM inventory WHERE invent_id = '" + Item.Invent_id + "'", connection);
                        {
                            //If it is in the database, error out
                            OdbcDataReader reader = Command1.ExecuteReader();
                            if (reader.HasRows)
                            {
                                webPage.WriteLine("<table border=1>");
                                webPage.WriteLine("<tr>"); //header row of table
                                webPage.WriteLine("<td>" + ID++ + "</td>");
                                webPage.WriteLine("<td>" + now + "</td>");
                                webPage.WriteLine("<td>" + Item.Item_ID + "</td>");
                                webPage.WriteLine("<td> already in database.</td>");
                            }
                            //If it is not in the database, add the item into the table
                            else
                            {
                                Console.WriteLine("Item {0} ", el.Attribute("item_id").Value + " was successfully added.");
                                Console.ReadLine();

                                OdbcCommand Command2 = new OdbcCommand("LOAD XML LOCAL INFILE 'C:/Users/abechtold/Documents/Update.xml' INTO TABLE item ROWS IDENTIFIED BY '<ADD>'", connection);
                                connection.Open();
                                OdbcDataReader reader2 = Command1.ExecuteReader();
                                connection.Close();
                            }
                            reader.Close();
                            reader.Dispose();
                        }
                        connection.Close();
                    }
                }
                //Check if the invent id is empty, if so, do not run any of the code for ADD
                else if (el.Attribute("invent_id") == null)
                {
                    Item.Item_ID = Convert.ToInt32(el.Attribute("item_id").Value);

                    webPage.WriteLine("<table border=1>");
                    webPage.WriteLine("<tr>"); //header row of table
                    webPage.WriteLine("<td>" + ID++ + "</td>");
                    webPage.WriteLine("<td>" + now + "</td>");
                    webPage.WriteLine("<td>" + Item.Item_ID + "</td>");
                    webPage.WriteLine("<td> not added: An input value was an improper datatype</td>");
                }
            }
        }
        webPage.WriteLine("</table>");  //end table
        webPage.WriteLine("</body>");
        webPage.WriteLine("</html>");

        webPage.Flush();//Make sure all characters are in the file and not buffered.
        webPage.Close();//Closes the file and officially writes it to disk.
    }

1 个答案:

答案 0 :(得分:1)

Crowcoder的评论帮助我回答了我的问题。因为我有

webPage.WriteLine("<table border=1>");

写在每个if,else语句中,它创建了自己的表行。通过从每个语句中删除此行,并在&#34;启动输出表中仅保留1&#34;点。这使得我的列排成一行并提供正确的间距。