C# - 根据原始文件名更改已转换文件的文件名

时间:2016-08-03 03:02:31

标签: c# excel

您好这是我想要做的,我想根据原始文件名.xls命名.dbf名称或转换后的文件。示例i转换acct_code.dbf,excel文件名应为acct_code.xls。我尝试了它,但它没有用。有人能帮我吗?怎么做?

enter image description here

这是代码。

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 + "converted_file.xls"; <--- here's the file name for excel file
            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);
                }
            }

        }

4 个答案:

答案 0 :(得分:2)

试试这个:

string originalFile = @"c:\Users\DonyaNenita\Desktop\Copro\install\acct_code.DBF";
string newFile = System.IO.Path.GetDirectoryName(originalFile) + "\\" + System.IO.Path.GetFileNameWithoutExtension(originalFile) + ".xls";

答案 1 :(得分:1)

Path类中有一个方法可以执行此操作

string oldFile = @"C:\folder1\folder2\file.dbf";
string newFile = Path.ChangeExtension(oldfile, ".xls");
// newFile is now C:\folder1\folder2\file.xls

答案 2 :(得分:1)

如果您重命名文件,请尝试以下操作:

System.IO.File.Move("oldfile.txt", "newfile.log");

但是如果你想要一个函数来重命名文件,那么使用:

public void RenameFile(string path, string name)
{
    System.IO.File.Move(Path.GetDirectoryName(filepath) + name + Path.GetExtension(filepath);
}

如果您只想要一个新名称,请使用:

string NewFileName = Path.GetDirectoryName(filepath) + "newfilename" + Path.GetExtension(filepath);

又一个功能:

public string GetPathWithNewName(string path, string newname)
{
    return Path.GetDirectoryName(path) + newname + Path.GetExtension(path);
}

答案 3 :(得分:1)

您还可以使用GetFileWithoutExtension方法:

string basename = System.IO.Path.GetFileNameWithoutExtension("foo.DBF");
string newFile = string.Join(".", basename, "xls");

我不确定您是否正在重命名或制作新文件......