c#保存excel以固定路径和文件名
SaveFileDialogsaveFileDialog=newSaveFileDialog();//创建对话框对象saveFileDialog.DefaultExt="...
SaveFileDialog saveFileDialog = new SaveFileDialog();//创建对话框对象
saveFileDialog.DefaultExt = "xls";
saveFileDialog.Filter = "EXCEL文件(*.XLS)|*.dbf";//指定文件的扩展名
saveFileDialog.FilterIndex = 0;
// saveFileDialog.FileName = cmbtbname.Text;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出到EXCEL";//设置标题
saveFileDialog.ShowDialog();
if (saveFileDialog.FileName == "")
return;
Stream myStream;
myStream = saveFileDialog.OpenFile();
这是saveFileDialog,急求高人帮忙解决,因为不想每次保存的时候都输入文件名
就是直接按指定的路径和文件名,比如以123的文件名存到c:\下 ,请详细一些,本人初学 最好能给出具体的代码,谢谢! 展开
saveFileDialog.DefaultExt = "xls";
saveFileDialog.Filter = "EXCEL文件(*.XLS)|*.dbf";//指定文件的扩展名
saveFileDialog.FilterIndex = 0;
// saveFileDialog.FileName = cmbtbname.Text;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出到EXCEL";//设置标题
saveFileDialog.ShowDialog();
if (saveFileDialog.FileName == "")
return;
Stream myStream;
myStream = saveFileDialog.OpenFile();
这是saveFileDialog,急求高人帮忙解决,因为不想每次保存的时候都输入文件名
就是直接按指定的路径和文件名,比如以123的文件名存到c:\下 ,请详细一些,本人初学 最好能给出具体的代码,谢谢! 展开
3个回答
展开全部
按日期和时间保存不就行了
//这里是路径文件名定义
{
System.DateTime date = System.DateTime.Now;
string time = date.ToString("yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo);
string path = @"C:\";
DirectoryInfo di = Directory.CreateDirectory(path + "\\GPRS_GB\\"+ time +"");
string file = textBox3.Text.Substring(textBox3.Text.LastIndexOf("\\"), 3);
string filepath = path + "GPRS_GB\\" + time;
Nsvc(file,filepath);
}
//这里是操作方法
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
private void Nsvc(string fileName,string filepath)
{
string mystring = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + textBox3.Text + ";Extended Properties=Excel 8.0";
OleDbConnection cnnxls = new OleDbConnection(mystring);
DataTable dt = new DataTable();
cnnxls.Open();
//textbox3.text 所要操作的excel完整的文件路径及文件名
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=product.mdb";
OleDbConnection conn = new OleDbConnection(connString);
conn.Open();
dt = cnnxls.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
String sheetName = dt.Rows[0]["TABLE_NAME"].ToString();
Excel.Application xlApp = new Excel.Application();
try
{
OleDbDataAdapter myDa = new OleDbDataAdapter("select * from [" + sheetName + "]", cnnxls);
DataSet DS = new DataSet();
myDa.Fill(DS);
xlApp.Application.DisplayAlerts = false;
object missing = System.Reflection.Missing.Value;
Excel.Workbooks workbooks = xlApp.Workbooks;
Excel.Workbook workbook = workbooks.Add(true);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
xlApp.Cells.Font.Size = 12;
xlApp.Visible = false;
//列名
for (int colIdx = 0; colIdx < DS.Tables[0].Columns.Count; colIdx++)
{
worksheet.Cells[1, colIdx+1] = DS.Tables[0].Columns[colIdx].ToString();
}
//中间操作
//.................
//表头
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, DS.Tables[0].Columns.Count + 4]).Cells.Interior.ColorIndex = 34;
////特殊列表头
//worksheet.get_Range(worksheet.Cells[1, 12], worksheet.Cells[1, 12]).Cells.Interior.ColorIndex = 38;
//worksheet.get_Range(worksheet.Cells[1, 3], worksheet.Cells[1, 3]).Cells.Interior.ColorIndex = 38;
//worksheet.get_Range(worksheet.Cells[1, 10], worksheet.Cells[1, 10]).Cells.Interior.ColorIndex = 38;
////特殊列
//worksheet.get_Range(worksheet.Cells[2, 3], worksheet.Cells[65536, 3]).Cells.Interior.ColorIndex = 6;
//worksheet.get_Range(worksheet.Cells[2, 10], worksheet.Cells[65536, 10]).Cells.Interior.ColorIndex = 6;
//worksheet.get_Range(worksheet.Cells[2, 12], worksheet.Cells[65536, 12]).Cells.Interior.ColorIndex = 6;
////新增列表头颜色
//worksheet.get_Range(worksheet.Cells[1, 16], worksheet.Cells[1, 16]).Cells.Interior.ColorIndex = 50;
//worksheet.get_Range(worksheet.Cells[1, 18], worksheet.Cells[1, 18]).Cells.Interior.ColorIndex = 50;
//原表字体
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[65536, DS.Tables[0].Columns.Count]).Cells.Font.Name = "Arial";
//原表表头字体大小
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, DS.Tables[0].Columns.Count]).Cells.Font.Size = 10;
//原表字体大小
worksheet.get_Range(worksheet.Cells[2, 1], worksheet.Cells[65536, DS.Tables[0].Columns.Count]).Cells.Font.Size = 8;
//全篇边框
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[65536, DS.Tables[0].Columns.Count+4]).Cells.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
//全篇单元格
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[65536, DS.Tables[0].Columns.Count+4]).Cells.ColumnWidth = 8;
worksheet.get_Range(worksheet.Cells[2, 1], worksheet.Cells[65536, DS.Tables[0].Columns.Count+4]).Cells.RowHeight = 17;
//表头单元格
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, DS.Tables[0].Columns.Count+4]).Cells.RowHeight = 60;
String tempfile = filepath;
String fullName = tempfile + fileName + "-"+ a +".xls";
workbook.SaveCopyAs(fullName);
//xlApp.Application.DisplayAlerts = true;
//xlApp.Visible = true;
IntPtr t = new IntPtr(xlApp.Hwnd);
int k = 0;
GetWindowThreadProcessId(t, out k);
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
p.Kill();
}
catch (Exception ex)
{
messageLabel.Text = ex.Message;
}
finally
{
cnnxls.Close();
conn.Close();
}
}
这是一个完整的对某个已存在的excel进行操作并另存的程序 除了内容 连杀进程也包括了 如果你要新建 则可以省去nsvc里前半部分内容 自己新建即可 其他照搬
7月21日修改 有问题就再提出 可以单独找我 qq1003019919 赶紧节分吧呵呵
//这里是路径文件名定义
{
System.DateTime date = System.DateTime.Now;
string time = date.ToString("yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo);
string path = @"C:\";
DirectoryInfo di = Directory.CreateDirectory(path + "\\GPRS_GB\\"+ time +"");
string file = textBox3.Text.Substring(textBox3.Text.LastIndexOf("\\"), 3);
string filepath = path + "GPRS_GB\\" + time;
Nsvc(file,filepath);
}
//这里是操作方法
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
private void Nsvc(string fileName,string filepath)
{
string mystring = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + textBox3.Text + ";Extended Properties=Excel 8.0";
OleDbConnection cnnxls = new OleDbConnection(mystring);
DataTable dt = new DataTable();
cnnxls.Open();
//textbox3.text 所要操作的excel完整的文件路径及文件名
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=product.mdb";
OleDbConnection conn = new OleDbConnection(connString);
conn.Open();
dt = cnnxls.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
String sheetName = dt.Rows[0]["TABLE_NAME"].ToString();
Excel.Application xlApp = new Excel.Application();
try
{
OleDbDataAdapter myDa = new OleDbDataAdapter("select * from [" + sheetName + "]", cnnxls);
DataSet DS = new DataSet();
myDa.Fill(DS);
xlApp.Application.DisplayAlerts = false;
object missing = System.Reflection.Missing.Value;
Excel.Workbooks workbooks = xlApp.Workbooks;
Excel.Workbook workbook = workbooks.Add(true);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
xlApp.Cells.Font.Size = 12;
xlApp.Visible = false;
//列名
for (int colIdx = 0; colIdx < DS.Tables[0].Columns.Count; colIdx++)
{
worksheet.Cells[1, colIdx+1] = DS.Tables[0].Columns[colIdx].ToString();
}
//中间操作
//.................
//表头
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, DS.Tables[0].Columns.Count + 4]).Cells.Interior.ColorIndex = 34;
////特殊列表头
//worksheet.get_Range(worksheet.Cells[1, 12], worksheet.Cells[1, 12]).Cells.Interior.ColorIndex = 38;
//worksheet.get_Range(worksheet.Cells[1, 3], worksheet.Cells[1, 3]).Cells.Interior.ColorIndex = 38;
//worksheet.get_Range(worksheet.Cells[1, 10], worksheet.Cells[1, 10]).Cells.Interior.ColorIndex = 38;
////特殊列
//worksheet.get_Range(worksheet.Cells[2, 3], worksheet.Cells[65536, 3]).Cells.Interior.ColorIndex = 6;
//worksheet.get_Range(worksheet.Cells[2, 10], worksheet.Cells[65536, 10]).Cells.Interior.ColorIndex = 6;
//worksheet.get_Range(worksheet.Cells[2, 12], worksheet.Cells[65536, 12]).Cells.Interior.ColorIndex = 6;
////新增列表头颜色
//worksheet.get_Range(worksheet.Cells[1, 16], worksheet.Cells[1, 16]).Cells.Interior.ColorIndex = 50;
//worksheet.get_Range(worksheet.Cells[1, 18], worksheet.Cells[1, 18]).Cells.Interior.ColorIndex = 50;
//原表字体
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[65536, DS.Tables[0].Columns.Count]).Cells.Font.Name = "Arial";
//原表表头字体大小
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, DS.Tables[0].Columns.Count]).Cells.Font.Size = 10;
//原表字体大小
worksheet.get_Range(worksheet.Cells[2, 1], worksheet.Cells[65536, DS.Tables[0].Columns.Count]).Cells.Font.Size = 8;
//全篇边框
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[65536, DS.Tables[0].Columns.Count+4]).Cells.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
//全篇单元格
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[65536, DS.Tables[0].Columns.Count+4]).Cells.ColumnWidth = 8;
worksheet.get_Range(worksheet.Cells[2, 1], worksheet.Cells[65536, DS.Tables[0].Columns.Count+4]).Cells.RowHeight = 17;
//表头单元格
worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, DS.Tables[0].Columns.Count+4]).Cells.RowHeight = 60;
String tempfile = filepath;
String fullName = tempfile + fileName + "-"+ a +".xls";
workbook.SaveCopyAs(fullName);
//xlApp.Application.DisplayAlerts = true;
//xlApp.Visible = true;
IntPtr t = new IntPtr(xlApp.Hwnd);
int k = 0;
GetWindowThreadProcessId(t, out k);
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
p.Kill();
}
catch (Exception ex)
{
messageLabel.Text = ex.Message;
}
finally
{
cnnxls.Close();
conn.Close();
}
}
这是一个完整的对某个已存在的excel进行操作并另存的程序 除了内容 连杀进程也包括了 如果你要新建 则可以省去nsvc里前半部分内容 自己新建即可 其他照搬
7月21日修改 有问题就再提出 可以单独找我 qq1003019919 赶紧节分吧呵呵
展开全部
saveFileDialog.FileName = "abc";
saveFileDialog.InitialDirectory = "c:\\";
加上这样就可以了
saveFileDialog.InitialDirectory = "c:\\";
加上这样就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
保存的时候直接默认文件名吧
或者设置参数,每次保存的时候就按参数来
或者设置参数,每次保存的时候就按参数来
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询