Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-根据原始文件名更改转换文件的文件名_C#_Excel - Fatal编程技术网

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

C#-根据原始文件名更改转换文件的文件名,c#,excel,C#,Excel,您好这就是我想要做的,我想要.xls名称,或者基于原始文件名.dbf将文件转换为名称。示例i转换acct\u code.dbf,excel文件名应为acct\u code.xls。我试过了,但没用。有人能帮我吗?如何做到这一点 这是代码 private void button1_Click(object sender, EventArgs e) { DialogResult result = openFileDialog1.ShowDialog(); /

您好这就是我想要做的,我想要
.xls
名称,或者基于原始文件名
.dbf
将文件转换为名称。示例i转换
acct\u code.dbf
,excel文件名应为
acct\u code.xls
。我试过了,但没用。有人能帮我吗?如何做到这一点

这是代码

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);
                }
            }

        }
private void按钮1\u单击(对象发送者,事件参数e)
{
DialogResult=openFileDialog1.ShowDialog();//显示对话框。
if(result==DialogResult.OK)//测试结果。
{
textBox1.Text=openFileDialog1.FileName;
}
}
私有无效按钮2\u单击(对象发送者,事件参数e)
{
string constr=“Provider=VFPOLEDB.1;数据源=“+Directory.GetParent(textBox1.Text).FullName;
字符串ExcelFileName=AppDomain.CurrentDomain.BaseDirectory+“converted_file.xls”尝试以下操作:

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

string originalFile = @"c:\Users\DonyaNenita\Desktop\Copro\install\acct_code.DBF";
string newFile = System.IO.Path.GetDirectoryName(originalFile) + "\\" + System.IO.Path.GetFileNameWithoutExtension(originalFile) + ".xls";
类中有一个方法可以执行此操作

string oldFile = @"C:\folder1\folder2\file.dbf";
string newFile = Path.ChangeExtension(oldfile, ".xls");
// newFile is now C:\folder1\folder2\file.xls
类中有一个方法可以执行此操作

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

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

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);
}

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

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);
}

您还可以使用
GetFileWithoutExtension
方法:

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

我不确定您是否正在重命名或创建新文件…

您也可以使用
GetFileWithoutExtension
方法:

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

我不确定您是否正在重命名或创建新文件…

“System.IO.Path.getFileName WithOutExtension(originalFile)”是键。谢谢兄弟:)使用System.IO.Path.Combine(directoryName,fileName)而不是directoryName+“\\”+fileName'System.IO.Path.getFileName WithOutExtension(originalFile)”是键。谢谢兄弟:)使用System.IO.Path.Combine(directoryName,fileName)而不是directoryName+“\\”+fileName这不回答问题,OP只想生成新文件名这不回答问题,OP只想生成新文件名