C#创建的excel文件怎么直接保存到网站根目录下的某个文件夹?

Microsoft.Office.Interop.Excel.Applicationexcel1=newMicrosoft.Office.Interop.Excel.Ap... Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();
Workbook workbook1 = excel1.Workbooks.Add(true);
Worksheet worksheet1 = (Worksheet)workbook1.Worksheets["sheet1"];
excel1.Visible = false;
把excel1 这个创建的excel直接保存的网站的某个根目录下的文件夹?
展开
 我来答
89801860
2011-10-18 · TA获得超过139个赞
知道小有建树答主
回答量:186
采纳率:100%
帮助的人:130万
展开全部
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);
}
}
追问
不对吧 我这是Web应用程序, SaveFileDialog是什么?
追答
上面代码是WinForm程式 通过对话框获取保存路径,也就是SaveFileDialog 
用到WEB程式中 你不要用 string savePath = saveFileDialogWin.FileName; 你直接給
string savePath=“網站根目錄\\存放EXCEL目錄”; 設定一個默認值就行了
再用 sheet.SaveAs(savePath, miss, miss, miss, miss, miss, Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss); 保存EXCEL就行了
wangbin45623
2011-10-18 · TA获得超过710个赞
知道小有建树答主
回答量:1216
采纳率:0%
帮助的人:481万
展开全部
Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();
Workbook workbook1 = excel1.Workbooks.Add(true);
Worksheet worksheet1 = (Worksheet)workbook1.Worksheets["sheet1"];
worksheet1 = (Worksheet)workbook1.Worksheets.Add(Type.Missing, workbook1.Worksheets[1], 1, Type.Missing);
worksheet1.Activate();
worksheet1.Cells[2, 2] = 3455555;
excel1.Visible = true;
excel1.DisplayAlerts = false;//不显示提示框
workbook1.Close(true, "d:\\1.xls", null);
//关闭
   worksheet1 = null;
workbook1 = null;
excel1.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel1);
excel1 = null;
System.GC.Collect();
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式