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

作者&投稿:班英 (若有异议请与网页底部的电邮联系)
C#创建的excel文件怎么直接保存到网站根目录下的某个文件夹?~

Worksheet sheet = (Worksheet)book.ActiveSheet;
string savePath = “” //你要保存的路径
.................................
................................
sheet.SaveAs(savePath, miss, miss, miss, miss, miss, Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss);

//下面附上全部代码,自己慢慢研究去,不同的是下面代码是 通过对话框获取保存路径,你改成默认路径就行了
private void ExportExcel(DataGridView dgv)
{
try
{
SaveFileDialog saveFileDialogWin = new SaveFileDialog();
saveFileDialogWin.Filter = "导出Excel(*.xls)|*.xls";
saveFileDialogWin.FilterIndex = 0;
saveFileDialogWin.RestoreDirectory = true;
saveFileDialogWin.CreatePrompt = false;
saveFileDialogWin.Title = "保存到";
saveFileDialogWin.ShowDialog();
string savePath = saveFileDialogWin.FileName;

Missing miss = Missing.Value;
ApplicationClass excel = new ApplicationClass();
excel.Application.Workbooks.Add(true);
excel.Visible = false;
Workbooks books = excel.Workbooks;
Workbook book = (Workbook)books.Add(miss);
Worksheet sheet = (Worksheet)book.ActiveSheet;
sheet.Name = "WorkSheet";

//写入表头
for (int i = 1; i < dgv.ColumnCount; i++)
{
excel.Cells[1, i] = dgv.Columns[i].HeaderText.ToString();
}

//填充内容
for (int i = 0; i < dgv.RowCount; i++)
{
for (int j = 1; j < dgv.ColumnCount; j++)
{

excel.Cells[i + 2, j] = dgv[j, i].Value.ToString().Trim();
}
}
sheet.SaveAs(savePath, miss, miss, miss, miss, miss, Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss);
book.Close(false, miss, miss);
books.Close();
excel.Quit();
Marshal.ReleaseComObject(sheet);
Marshal.ReleaseComObject(book);
Marshal.ReleaseComObject(books);
Marshal.ReleaseComObject(excel);
GC.Collect();
}
catch(Exception e)
{
MessageBox.Show("导出失败," + e.Message);
}
}

fp=fopen(filename,"wb");
里的filename就表示了文件的路径及文件名,所以要把输入的文件名和文件路径拼接起来,计算出这个filename
最好过滤一下别让路径和文件名中有非法字符,比如:\/+什么的。
scanf也限制一下长度。
scanf("%19s",filename);
scanf("%19s",path);
参考如下:
#include
#include
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("
to end input ,press Ctrl+Z in newline,then ENTER:
");
if((fp=fopen(buffer,"wt+"))==NULL)
{
printf("no such path,
strike any key exit!");
getch();
exit(1);
}
while ((ch=getchar())!=-1) ch=fputc(ch,fp);
fclose(fp);
printf("==================================

");
printf("file path \"%s\"
file name \"%s\":
and its content:
",path,filename);
fp=fopen(buffer,"rt");
while ((ch=fgetc(fp))!=-1) putchar(ch);
fclose(fp);
return 0;

按日期和时间保存不就行了
//这里是路径文件名定义
{
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:\\";
加上这样就可以了

保存的时候直接默认文件名吧

或者设置参数,每次保存的时候就按参数来


澳门特别行政区17589562308: c#保存excel以固定路径和文件名 -
雷馨复方: 按日期和时间保存不就行了 //这里是路径文件名定义 { System.DateTime date = System.DateTime.Now; string time = date.ToString("yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo); string path = @"C:\"; DirectoryInfo di = Directory....

澳门特别行政区17589562308: c# 如何把EXCEL对象保存到某个路径中的EXCEL文件中(不要保存对话框) -
雷馨复方: 1、用客户端Response保存2、用office的com组件保存

澳门特别行政区17589562308: C#创建的excel文件怎么直接保存到网站根目录下的某个文件夹? -
雷馨复方: Worksheet sheet = (Worksheet)book.ActiveSheet; string savePath = “” //你要保存的路径.................................................................sheet.SaveAs(savePath, miss, miss, miss, miss, miss, Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss)...

澳门特别行政区17589562308: C#导出Excel格式文件 实现的几种方法 -
雷馨复方: 1、第一种:使用 Microsoft.Office.Interop.Excel.dll 第一种方法性能实在是不敢恭维,而且局限性太多.首先必须要安装 office(如果计算机上面没有的话),而且导出时需要指定文件保存的路径.也可以输出到浏览器下载,当然前提是已经保存...

澳门特别行政区17589562308: C#读取EXCEL中的信息,并保存到数据库 -
雷馨复方: 在EXCEL中可以定义表的,定义名称的步骤很简单:1.选择好你要的目的工作区域(即涂黑.如要选择不连续的区域,请按住“Ctrl”键.)2.用鼠标点一下 “名称框”,让光标进入该输入框,然后输入任何你想用的名称后按 “Enter”键....

澳门特别行政区17589562308: 如何用C#导出Excel表格 -
雷馨复方: 编辑代码1打开Microsoft VisualStudio 2010, 新建名字为【导出excel】的程序.在程序界面上放置合适的控件:包括,日志输出文本框控件;用于显示原始表格数据的listview控件;2个控制按钮,分别是【添加数据】和【导出数据】.因为是使...

澳门特别行政区17589562308: C# 如何获取指定路径下的Excel文件并转换成DataTable -
雷馨复方: 拣出来给你了,自己稍微修改下./// <summary>/// 根据传入的Excel表输出Datatable/// </summary>/// <param name="strExcelFileName">Excel表路径及文件名称</param>/// <param name="strTableName">数据表名</param>/// <...

澳门特别行政区17589562308: C# 将excel表格读取出来,向里面添加数据 保存 -
雷馨复方: //建立Excel对象 Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); excel.Application.Workbooks.Add(true); 后面可以参考 //生成字段名称 for (int i = 0; i

澳门特别行政区17589562308: c#.net中,如何设置excel表的路径? -
雷馨复方: 你说的是什么路径,工作表(sheet1)还是xls文件 ,那跟一般的访问文件不是一样吗?"D:\\f目录\\1.xls"或@"D:\目录\1.xls"

澳门特别行政区17589562308: C#保存EXCEL方法中,十几个属性参数都填些什么呀?解决后,再送70分 -
雷馨复方: 一般只需要第一个参数就可以了:Workbook.SaveAs(Server.MapPath(".")+"filename.xls"); 参数 Filename 要保存的文件的名称.可以包含完整路径;否则,Microsoft Office Excel 将文件保存在当前文件夹中. FileFormat 保存文件时要使...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网