c#保存excel以固定路径和文件名
发布网友
发布时间:2023-02-24 23:44
我来回答
共3个回答
热心网友
时间:2023-06-28 06:27
按日期和时间保存不就行了
//这里是路径文件名定义
{
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=proct.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 赶紧节分吧呵呵
热心网友
时间:2023-06-28 06:27
saveFileDialog.FileName = "abc";
saveFileDialog.InitialDirectory = "c:\\";
加上这样就可以了
热心网友
时间:2023-06-28 06:28
保存的时候直接默认文件名吧
或者设置参数,每次保存的时候就按参数来