[英]How to copy specific sheet name from excel and save it to a new location? Using C#
[英]How to save an excel file to a specific location C#?
这是一个使用 .NET 框架的 Windows 表单应用程序,它将记录保存到 MS 数据库中。 还可以选择将数据库导出为 excel 文件。
这是我的“创建 excel 文件”按钮单击 function:
private void button5_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from [dbo].[Table]";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet("New_DataSet");
ds.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;
da.Fill(dt);
ds.Tables.Add(dt);
ExcelLibrary.DataSetHelper.CreateWorkbook("MyExcelFile.xls",ds);
MessageBox.Show("Archivo excel generado correctamente.");
con.Close();
}
使用我发布的应用程序版本,注意到 excel 文件是在“C:\Users\userName\AppData\Local\Apps\2.0\ZDTK7LMQ.YEZ\V0BOPT21.N23”中创建的我的默认值。
我想更改保存位置。 有什么线索吗?
PD:我使用的 excel 库是“ExcelLibrary”。
using ExcelLibrary.CompoundDocumentFormat;
using ExcelLibrary.SpreadSheet;
using ExcelLibrary.BinaryDrawingFormat;
using ExcelLibrary.BinaryFileFormat;
如果您尝试在文件名前添加 \ 一个斜杠或两个斜杠 \ 并查看是否有所不同:“\MyExcelFile.xls”或“\ \ MyExcelFile.xls”。 此外,您可能希望取出文件名并将文件名和路径构造为字符串,然后将字符串添加到调用中,如下所示:
var path = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "desired_dir_name"); // bin, etc
string myFileNameAndPath = path + "\\" + "MyExcelFile.xls";
ExcelLibrary.DataSetHelper.CreateWorkbook(myFileNameAndPath ,ds);
通过上面运行调试器以查看设置了哪些路径并根据您的需要修改位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.