导出Excel—外部表不是预期的格式

方法一:

http://www.liqiu.info/2011/01/%E5%88%86%E4%BA%AB-net%E5%AF%BC%E5%87%BAexcel/

 

http://www.google.com.hk/#hl=zh-CN&newwindow=1&safe=strict&q=%E5%AF%BC%E5%87%BA+%E9%9D%9E+html%E7%9A%84+excel+&oq=%E5%AF%BC%E5%87%BA+%E9%9D%9E+html%E7%9A%84+excel+&aq=f&aqi=&aql=&gs_sm=e&gs_upl=104042l112685l10l8l8l0l0l0l1l315l1735l0.3.4.1l8&fp=1&biw=1680&bih=932&cad=b

方法二:

最终是用DataGrid通过Response导出成Excel格式,在页面直接打开,然后让用户另存为正式Excel格式,再导入就可以了

Response出来的就是这个问题,虽然他生成excel很快,但再导入数据库的时候就有毛病,还是用excel.dll控件吧,这个绝对没有问题 
public void Out2Excel(string sTableName,string url) 

Excel.Application oExcel=new Excel.Application(); 
Workbooks oBooks; 
Workbook oBook; 
Sheets oSheets; 
Worksheet oSheet; 
Range oCells; 
string sFile= " ",sTemplate= " "; 
// 
System.Data.DataTable dt=TableOut(sTableName).Tables[0]; 

sFile=url+ "\\myExcel.xls "; 
sTemplate=url+ "\\MyTemplate.xls "; 
// 
oExcel.Visible=false; 
oExcel.DisplayAlerts=false; 
//定义一个新的工作簿 
oBooks=oExcel.Workbooks; 
oBooks.Open(sTemplate,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing, Type.Missing, Type.Missing); 
oBook=oBooks.get_Item(1); 
oSheets=oBook.Worksheets; 
oSheet=(Worksheet)oSheets.get_Item(1); 
//命名该sheet 
oSheet.Name= "Sheet1 "; 

oCells=oSheet.Cells; 
//调用dumpdata过程,将数据导入到Excel中去 
DumpData(dt,oCells); 
//保存 
oSheet.SaveAs(sFile,Excel.XlFileFormat.xlTemplate,Type.Missing,Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing); 
oBook.Close(false, Type.Missing,Type.Missing); 
//退出Excel,并且释放调用的COM资源 
oExcel.Quit(); 

GC.Collect(); 
KillProcess( "Excel "); 


private void KillProcess(string processName) 

System.Diagnostics.Process myproc= new System.Diagnostics.Process(); 
//得到所有打开的进程 
try 

foreach (Process thisproc in Process.GetProcessesByName(processName)) 

if(!thisproc.CloseMainWindow()) 

thisproc.Kill(); 



catch(Exception Exc) 

throw new Exception( " ",Exc); 

}

你可能感兴趣的:(导出Excel—外部表不是预期的格式)