c#保存excel以固定路径和文件名

c#保存excel以固定路径和文件名,第1张

按日期和时间保存不就行了

//这里是路径文件名定义

{

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.0Data 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 = 0colIdx <DS.Tables[0].Columns.CountcolIdx++)

{

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 赶紧节分吧呵呵

fp=fopen(filename,"wb")

里的filename就表示了文件的路径及文件名,所以要把输入的文件名和文件路径拼接起来,计算出这个filename

最好过滤一下别让路径和文件名中有非法字符,比如:\/+<>什么的。

scanf也限制一下长度。

scanf("%19s",filename)

scanf("%19s",path)

参考如下:

#include <stdio.h>

#include <stdlib.h>

int main(void)

{

FILE *fp

char ch, filename[20], path[20],buffer[40]

printf("Enter the file name: ")

scanf("%s",filename)

printf("Enter the path: ")

scanf("%s",path)

sprintf(buffer, "%s\\%s", path,filename)

printf("\nto end input ,press Ctrl+Z in newline,then ENTER:\n")

if((fp=fopen(buffer,"wt+"))==NULL)

{

printf("no such path, \nstrike any key exit!")

getch()

exit(1)

}

while ((ch=getchar())!=-1) ch=fputc(ch,fp)

fclose(fp)

printf("==================================\n\n")

printf("file path \"%s\" \nfile name \"%s\":\nand its content:\n",path,filename)

fp=fopen(buffer,"rt")

while ((ch=fgetc(fp))!=-1) putchar(ch)

fclose(fp)

return 0


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/8146560.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-13
下一篇 2023-04-13

发表评论

登录后才能评论

评论列表(0条)

保存