以下是我的一个把数据保存在Excel的类:
public static bool ExportToExcel(DataTable DataDt)
{
SaveFileDialog dlgSaveFile=new SaveFileDialog();
dlgSaveFile.OverwritePrompt=true;
dlgSaveFile.Filter="Excel files(*.xls)|*.xls";
if(dlgSaveFile.ShowDialog() == DialogResult.OK)
{
int k=dlgSaveFile.FileName.LastIndexOf("\\",dlgSaveFile.FileName.Length);
string filePath=dlgSaveFile.FileName.Substring(0,k).ToString();
int c=dlgSaveFile.FileName.LastIndexOf(".",dlgSaveFile.FileName.Length);
int d=c-k;
string fName=dlgSaveFile.FileName.Substring(k+1,d).ToString();
Excel.Application excel= new Excel.Application();
int rowIndex=1;
int colIndex=0;
excel.Application.Workbooks.Add(true);
System.Data.DataTable table=DataDt ;
foreach(DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[1,colIndex]=col.ColumnName;
}
foreach(DataRow row in table.Rows)
{
rowIndex++;
colIndex=0;
foreach(DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
}
}
string p_filename = dlgSaveFile.FileName;
excel.Visible=false;
excel.ActiveWorkbook.SaveAs(p_filename,Excel.XlFileFormat.xlExcel9795,null,null,false,false,Excel.XlSaveAsAccessMode.xlNoChange,null,null,null,null);
excel.Quit();
excel=null;
GC.Collect();
return true;
}
else
{
return false;
}
}
现在存在以下问题:
1,导出的时候如果该文件已存在,会弹出两次“是否替换对话框?“;
2,谁能告诉我这个类里面哪段话是导出到Excel的,哪段话是让Excel保存的?
3,不管是有一条数据还是有很多条数据,我一点击保存按钮,屏幕都要定格很长时间,
为何如此之慢?就像失去响应一样;
public static bool ExportToExcel(DataTable DataDt)
{
SaveFileDialog dlgSaveFile=new SaveFileDialog();
dlgSaveFile.OverwritePrompt=true;
dlgSaveFile.Filter="Excel files(*.xls)|*.xls";
if(dlgSaveFile.ShowDialog() == DialogResult.OK)
{
int k=dlgSaveFile.FileName.LastIndexOf("\\",dlgSaveFile.FileName.Length);
string filePath=dlgSaveFile.FileName.Substring(0,k).ToString();
int c=dlgSaveFile.FileName.LastIndexOf(".",dlgSaveFile.FileName.Length);
int d=c-k;
string fName=dlgSaveFile.FileName.Substring(k+1,d).ToString();
Excel.Application excel= new Excel.Application();
int rowIndex=1;
int colIndex=0;
excel.Application.Workbooks.Add(true);
System.Data.DataTable table=DataDt ;
foreach(DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[1,colIndex]=col.ColumnName;
}
foreach(DataRow row in table.Rows)
{
rowIndex++;
colIndex=0;
foreach(DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
}
}
string p_filename = dlgSaveFile.FileName;
excel.Visible=false;
excel.ActiveWorkbook.SaveAs(p_filename,Excel.XlFileFormat.xlExcel9795,null,null,false,false,Excel.XlSaveAsAccessMode.xlNoChange,null,null,null,null);
excel.Quit();
excel=null;
GC.Collect();
return true;
}
else
{
return false;
}
}
现在存在以下问题:
1,导出的时候如果该文件已存在,会弹出两次“是否替换对话框?“;
2,谁能告诉我这个类里面哪段话是导出到Excel的,哪段话是让Excel保存的?
3,不管是有一条数据还是有很多条数据,我一点击保存按钮,屏幕都要定格很长时间,
为何如此之慢?就像失去响应一样;
解决方案 »
- 数据表列相加问题
- DefaultView.RowFilter不能成功过滤
- 如何对摄像头传来的实时视频截图
- 100 分, .net2005 的winForm 控件在那里有比较好的免费下载的呀,急
- C#连接oracle 出现的版本不同的问题
- 在window怎样实现LDAP?!!!!?!!!分不够再加!
- Visual Studio .net 的安装组件有哪儿些?
- 请问如何象QQ那样拖曳到屏幕边上时缩到边上去?
- 各位,关于递归算法的模型,理论
- c#初学者的几个问题!先答先得分,答得好可加分
- 《龙与地下城OL》是用.NET开发的么?
- 导出到Excel时,数据超过了65536时,怎样生成另一张Sheet表,将数据导入其中呢?(C#版的)
2 excel.Cells[1,colIndex]=col.ColumnName;
excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString
这些是写到Excel里
3 这个好像没有,数据少时很快呀 是不是其它地方出问题了
这个是导出
public bool saveAsExcel(String xlsOutput)
{
m_oBook.SaveAs(xlsOutput, Excel.XlFileFormat.xlWorkbookNormal, m_oMissing, m_oMissing, m_oMissing, m_oMissing,Excel.XlSaveAsAccessMode.xlNoChange, m_oMissing, m_oMissing, m_oMissing, m_oMissing, m_oMissing); return true;
}
#endregion在参数中指定如果文件存在 可以替换