只有一半的dbf数据被转换为xls

时间:2016-08-03 04:34:38

标签: c# excel

您好我试图将dbf转换为xls,但问题是只转换了一半的数据。有人可以帮助我,发现这里有什么问题吗?

以下是我要转换的dbf的示例图片。 enter image description here

这里转换的文件只有2个数据 enter image description here

更新 这是另一个要求的样本。

我转换这个dbf文件。 enter image description here

这是结果 enter image description here

这是代码。

  static Missing mv = Missing.Value;
        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
            if (result == DialogResult.OK) // Test result.
            {
                textBox1.Text = openFileDialog1.FileName;



            }
        }

 private void button2_Click(object sender, EventArgs e)
        {
            string constr = "Provider=VFPOLEDB.1;Data Source=" + Directory.GetParent(textBox1.Text).FullName;
            string ExcelFileName = AppDomain.CurrentDomain.BaseDirectory + System.IO.Path.GetFileNameWithoutExtension(textBox1.Text) + ".xls";
            using (OleDbConnection con = new OleDbConnection(constr))
            {
                var sql = "select * from " + Path.GetFileName(textBox1.Text) + ";";
                OleDbCommand cmd = new OleDbCommand(sql, con);
                DataTable dt = new DataTable();
                try
                {
                    con.Open();
                }
                catch (Exception ex)
                {

                    MessageBox.Show("Error connecting database: " + ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                if (con.State == ConnectionState.Open)
                {
                    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                    MessageBox.Show("Reading database...  ", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    da.Fill(dt);
                    MessageBox.Show("Completed.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                if (con.State == ConnectionState.Open)
                {
                    try
                    {
                        con.Close();
                    }
                    catch { }
                }

                if (dt != null && dt.Rows.Count > 0)
                {
                    GenerateExcel(dt, ExcelFileName);
                }
            }

        }

static void GenerateExcel(DataTable sourceDataTable, string ExcelFileName)
        {
            MessageBox.Show("Generating Excel File...", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);


            Excel.Application excelApp = new Excel.Application();
            Excel.Workbook wkb = excelApp.Workbooks.Add(mv);
            Excel.Worksheet wks = wkb.Sheets[1];

            for (int i = 0; i < sourceDataTable.Columns.Count; ++i)
            {
                ((Excel.Range)wks.Cells[1, i + 1]).Value = sourceDataTable.Columns[i].ColumnName;
            }
            Excel.Range header = wks.get_Range((object)wks.Cells[1, 1], (object)wks.Cells[1, sourceDataTable.Columns.Count]);
            header.EntireColumn.NumberFormat = "@";

            object[,] sourceDataTableObjectArray = new object[sourceDataTable.Rows.Count, sourceDataTable.Columns.Count];
            for (int row = 0; row < sourceDataTable.Rows.Count; ++row)
            {
                for (int col = 0; col < sourceDataTable.Columns.Count; ++col)
                {
                    sourceDataTableObjectArray[row, col] = sourceDataTable.Rows[row][col].ToString();
                }
            }
            ((Excel.Range)wks.get_Range((object)wks.Cells[2, 1], (object)wks.Cells[sourceDataTable.Rows.Count, sourceDataTable.Columns.Count])).Value2 = sourceDataTableObjectArray;
            header.EntireColumn.AutoFit();
            header.Font.Bold = true;
            wks.Application.ActiveWindow.SplitRow = 1;
            wks.Application.ActiveWindow.FreezePanes = true;
            wks.SaveAs(ExcelFileName, Excel.XlFileFormat.xlExcel8, mv, mv, mv, mv, mv, mv, mv, mv);
            wks = null;
            wkb = null;
            excelApp.Quit();
            MessageBox.Show("Completed.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }

1 个答案:

答案 0 :(得分:1)

一个问题可以解释为什么最后一行会被删除:

((Excel.Range)wks.get_Range((object)wks.Cells[2, 1], (object)wks.Cells[sourceDataTable.Rows.Count, sourceDataTable.Columns.Count])).Value2 = sourceDataTableObjectArray;

应改为

((Excel.Range)wks.get_Range((object)wks.Cells[2, 1], (object)wks.Cells[sourceDataTable.Rows.Count + 1, sourceDataTable.Columns.Count])).Value2 = sourceDataTableObjectArray;

说明:Excel范围不够大(1太小)无法接受完整的sourceDataTable。

至于其他问题 (见例1) - 问题出在数据库中。你的失踪&#34;数据不包含在数据库查询返回中,因为这些行标记为&#34;已删除&#34;在.dbf文件中。来源:

Manipulate the 'deleted record' flag on DBF files

让我相信你可以通过改变来解决你的问题

var sql = "select * from " + Path.GetFileName(textBox1.Text) + ";";

string tableName = Path.GetFileName(textBox1.Text)
var sql = "select * from Deleted(" + tableName + ") UNION select * from " + tableName + ";";

我真的不是你的dbf数据库和sql查询的专家,如果这不起作用你应该把它作为一个新问题发布。