如何在Visual Studio 2010中使用Crystal Reports创建报表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在Visual Studio 2010中使用Crystal Reports创建报表相关的知识,希望对你有一定的参考价值。

我是Visual Studio 2010 C#的新手。我正在创建一个创建报告的应用程序。报告中显示的信息来自mysql服务器。我已经安装了Crystal Reports。但是,我从MySQL获取数据时遇到了一些问题,因为我无法从MySQL中找到数据。报告创建向导中显示的数据不是MySQL中的数据库文件,而是我在C#中创建的表单。请帮忙。

截图:

答案

Here I找到了适合你问题的解决方案。

Crystal Reports可以使用ODBC DSN连接到数据库,您可以从中提取数据和信息以进行报告。

注意某些版本的Crystal Reports存在一个已知问题,即应用程序无法通过ODBC连接打开和浏览表和字段。在将Crystal Reports与MySQL一起使用之前,请确保您已更新到最新版本,包括任何未完成的Service Pack和修补程序。有关此问题的详细信息,请参阅“业务”对象知识库以获取更多信息。

跟着这些步骤:

  1. 使用数据源(ODBC)工具创建DSN。您可以指定完整的数据库,包括用户名和密码,也可以构建基本DSN并使用Crystal Reports设置用户名和密码。
  2. 通过单击“起始页”上的选项启动“交叉表”报告向导。展开“创建新连接”文件夹,然后展开“ODBC(RDO)”文件夹以获取ODBC数据源列表。
另一答案
    DataTable dataTable = new DataTable();

     int count1,count2;
        clsInfor.northCON.Open();
        clsInfor.dataAdapter = new SqlDataAdapter("SELECT Discontinued, QuantityPerUnit FROM Products", clsInfor.northCON);
        dataTable = new DataTable();
        clsInfor.dataAdapter.Fill(dataTable);
        Excel.Application app = new Excel.Application(); //creating a new application
        app.Visible = true;

        Excel.Workbook book = app.Workbooks.Add(1); // creating an instance of the workbook 
        Excel.Worksheet sheet = (Excel.Worksheet)book.Worksheets[1]; // creating an instance of the worksheet

        ((Excel.Range)sheet.Cells[1, "A"]).Value2 = "Report"; // creating the header of the report
        ((Excel.Range)sheet.Cells[2, "B"]).Value2 = "Number of products per Cat";//creating the names of the colomns in the excell spreedsheet
        ((Excel.Range)sheet.Cells[2, "C"]).Value2 = "Number of products that have been discontinued";

        ((Excel.Range)sheet.Cells[4, "D"]).Value2 = "Tot number of Prod";

        for (count1 = 0; count1 < dataTable.Rows.Count; count1++)
        {
            for (count2 = 0; count2 < dataTable.Columns.Count; count2++)
            {
                sheet.Cells[count1 + 3, count2 + 2] = dataTable.Rows[count1][count2];
            }
            sheet.Cells.Columns.AutoFit();
        }
另一答案
    private void btndlt_Click(object sender, EventArgs e)
    {

        DataGridViewRow row = dataGridView1.Rows[0];
        string id = row.Cells[0].Value.ToString();
        string path = @"";
        XmlDocument doc = new XmlDocument();
        doc.Load(path);
        XmlNode node = doc.SelectSingleNode("/TheToDoList/item[todoID='" + id + "']");
        node.ParentNode.RemoveChild(node);
        doc.Save(path);
        MessageBox.Show("Selected Record Deleted Successfully");
    }

    private void btnUpdate_Click(object sender, EventArgs e)
    {
        DataGridViewRow row = dataGridView1.Rows[0];
        int id1 = Convert.ToInt32(row.Cells[0].Value);
        string path = @"";
        XmlDocument doc = new XmlDocument();
        doc.Load(path);
        XmlNode node = doc.SelectSingleNode("/TheToDoList/item[todoID='" + id1 + "']");
        node.ParentNode.RemoveChild(node);
        doc.Save(path);

                //    //Load the XML File
                    doc.Load(path);


                    XmlElement root = doc.CreateElement("TheToDoList");
                    XmlElement Subroot = doc.CreateElement("item");
                    XmlElement todoID = doc.CreateElement("todoID");
                    XmlElement todoDate = doc.CreateElement("todoDate");
                    XmlElement todoTime = doc.CreateElement("todoTime");
                    XmlElement todoItem = doc.CreateElement("todoItem");
                    XmlElement todoStatus = doc.CreateElement("todoStatus");
                    XmlElement UserID = doc.CreateElement("UserID");
                    //Add the values for each nodes
                    todoID.InnerText = row.Cells[0].ToString();
                    todoDate.InnerText = row.Cells[1].ToString();
                    todoTime.InnerText = row.Cells[2].ToString();
                    todoItem.InnerText = row.Cells[3].ToString();
                    todoStatus.InnerText = row.Cells[4].ToString();
                    UserID.InnerText = row.Cells[5].ToString();
                    //Construct the document
                    doc.AppendChild(root);
                    root.AppendChild(Subroot);
                    Subroot.AppendChild(todoID);
                    Subroot.AppendChild(todoDate);
                    Subroot.AppendChild(todoTime);
                    Subroot.AppendChild(todoItem);
                    Subroot.AppendChild(todoStatus);
                    Subroot.AppendChild(UserID);
                    doc.Save(path);
                    MessageBox.Show("Selected Record Edited Successfully");
另一答案
        XmlDocument xmlDocument = new XmlDocument();
        string xmlFileName = (@"C:");
        xmlDocument.Load(xmlFileName);

        XmlElement xItem = xmlDocument.CreateElement("item");


        XmlElement xmlSubElement1 = xmlDocument.CreateElement("todoID");
        xmlSubElement1.InnerText = todoID;
        xItem.AppendChild(xmlSubElement1);

        XmlElement xmlSubElement2 = xmlDocument.CreateElement("todoDate");
        xmlSubElement2.InnerText = todoDate;
        xItem.AppendChild(xmlSubElement2);

        XmlElement xmlSubElement3 = xmlDocument.CreateElement("todoTime");
        xmlSubElement3.InnerText = todoTime;
        xItem.AppendChild(xmlSubElement3);

        XmlElement xmlSubElement4 = xmlDocument.CreateElement("todoItem");
        xmlSubElement4.InnerText = todoItem;
        xItem.AppendChild(xmlSubElement4);

        XmlElement xmlSubElement5 = xmlDocument.CreateElement("todoStatus");
        xmlSubElement5.InnerText = todoStatus;
        xItem.AppendChild(xmlSubElement5);

        XmlElement xmlSubElement6 = xmlDocument.CreateElement("UserID");
        xmlSubElement6.InnerText = UserID;
        xItem.AppendChild(xmlSubElement6);


        //xmlDocument.AppendChild(xItem);
        xmlDocument.DocumentElement.LastChild.AppendChild(xItem);
        // then finally save
        xmlDocument.Save(@"C:");
        xmlmsg ="A new item has been added to the to do list";
另一答案
DataAccess.Connection.Close();
DataAccess.Connection.Open();
DataAccess.dataAdapter = new SqlDataAdapter(" SELECT sales.qty, sales.ord_date, sales.payterms, stores.stor_name, stores.stor_id, titles.title_id, titles.title, titles.price, (sales.qty * titles.price) AS Total FROM sales INNER JOIN stores ON sales.stor_id = stores.stor_id INNER JOIN  titles ON sales.title_id = titles.title_id", DataAccess.Connection);

DataAccess.dataTable = new DataTable();
DataAccess.dataAdapter.Fill(DataAccess.dataTable);
Bind.DataSource = DataAccess.dataTable;
string temp = DataAccess.dataTable.Rows[0][3].ToString();

Excel.Application xlApp2 = new Excel.Application();
xlApp2.Visible = true;
Excel.Workbook Workbook2 = xlApp2.Workbooks.Add(1);
Excel.Worksheet Worksheet2 = (Excel.Worksheet)Workbook2.Sheets[1];

Worksheet2.Cells[1, 1] = "Sales Report";
Worksheet2.Cells[3, 1] = temp.ToUpper();
Worksheet2.Cells[4, 2] = "Order Date";
Worksheet2.Cells[4, 3] = "Payment Terms";
Worksheet2.Cells[4, 4] = "Store Name";
Worksheet2.Cells[4, 5] = "Store ID";
Worksheet2.Cells[4, 6] = "Title ID";
Worksheet2.Cells[4, 7] = "Book Title";
Worksheet2.Cells[4, 8] = "Unit Price";
Worksheet2.Cells[4, 9] = "Total Price";

int intCount2 = 5;
int TotalRec = 0;
int finecort = 0;
decimal cost = 0;
decimal cost1 = 0;

for (int n = 0; n < DataAccess.dataTable.Rows.Count; n++)
{
    if (DataAccess.dataTable.Rows[n][3].ToString().Equals(temp))
    {
        Worksheet2.Cells[intCount2, "C"] = DataAccess.dataTable.Rows[n][0];
        Worksheet2.Cells[intCount2, "B"] = DataAccess.dataTable.Rows[n][1];
        Worksheet2.Cells[intCount2, "D"] = DataAccess.dataTable.Rows[n][3];
        Worksheet2.Cells[intCount2, "E"] = DataAccess.dataTable.Rows[n][4];
        Worksheet2.Ce

以上是关于如何在Visual Studio 2010中使用Crystal Reports创建报表的主要内容,如果未能解决你的问题,请参考以下文章

电脑里的visual studio 2010怎么打开

如何在 Visual Studio 2010 中使用 Crystal Reports 创建报表

visual studio 2010 序列号在哪输入

如何使用Visual Studio2010运行C语言

如何在 Visual Studio 2010 中正确引用 dll?

visual studio 2010怎么运行程序?