就是按模板导出:http://download.csdn.net/detail/flag5418/1573880
解决方案 »
- 一个窗口关闭时如何设置另外一个窗口enable=true?
- 请问大家。.net如何读取oracle的自定义类型。
- 请问为什么在vs2005下测试没问题,没有登录就会产生不能登陆并返回
- 帮忙修改以下代码:关于远程获取的代码
- 載入Global出錯,請大家幫幫忙看看是怎么回事?
- 类型是hidden的文本框怎么用 webbrowser赋值
- 請問各位大哥,在sql 2000中有沒有什麼函數可以統計一個字符在另一個字串中出現了多少次???
- mcisendString 回调问题
- 请教,我想用C#开发软件,手头应放哪几本必须的参考书
- 在Windows窗体中,怎样得到DataGrid的当前选定记录中的值?
- .net连接ORACLE不支持WITH
- 未处理的“System.FormatException”类型的异常出现在 mscorlib.dll 中, 其他信息:Input string was not
/*
* 获取保存EXCEL的路径
*/
saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出文件保存路径";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
//strName储存保存EXCEL路径
string strName = saveFileDialog.FileName;
if (strName.Length != 0)
{
System.Reflection.Missing miss = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
excel.Application.Workbooks.Add(true); ;
//若是true,则在导出的时候会显示EXcel界面。
excel.Visible = false;
if (excel == null)
{
MessageBox.Show("EXCEL无法启动!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Microsoft.Office.Interop.Excel.Workbooks books = (Microsoft.Office.Interop.Excel.Workbooks)excel.Workbooks;
Microsoft.Office.Interop.Excel.Workbook book = (Microsoft.Office.Interop.Excel.Workbook)(books.Add(miss));
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.ActiveSheet;
//生成字段名称,逐条写,无效率
excel.Cells.Select();
for (int i = 1; i < gridView.ColumnCount; i++)
{
excel.Cells[2, i + 1] = gridView.Columns[i].HeaderText.ToString();
}
//以下为填充数据关键代码,逐条写,无效率
for (int i = 0; i < gridView.RowCount; i++)
{
for (int j = 0; j < gridView.ColumnCount; j++)
{
Range contentRange = excel.Cells[i + 3, j + 1] as Range;//获取单元格
if (gridView[j, i].Value == typeof(string))
{
excel.Cells[i + 3, j + 1] = "" + gridView[i, j].Value.ToString();
}
else
{
excel.Cells[i + 3, j + 1] = gridView[j, i].Value.ToString();
}
} System.Windows.Forms.Application.DoEvents(); } sheet.SaveAs(strName, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss);
book.Close(false, miss, miss);
books.Close();
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
System.Runtime.InteropServices.Marshal.ReleaseComObject(books);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
GC.Collect();
}
}