您好這是我想要做的,我想.xls
名稱或轉換文件的名稱基於原始文件名.dbf
。例如我轉換acct_code.dbf
,excel的文件名應該是acct_code.xls
。我嘗試concat它,但它沒有工作。有人能幫我嗎?這個怎麼做?C# - 基於原始文件名更改轉換文件的文件名
這裏是代碼。
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);
}
}
}
'System.IO.Path.GetFileNameWithoutExtension(originalFile)'是關鍵。感謝兄弟:) – nethken
使用System.IO.Path.Combine(directoryName,fileName)而不是directoryName +「\\」+ fileName – Den