winform导出Excel
将Winform的表单导入到Excel。
界面类似于移动缴费单的形式,要整体导入。
[解决办法]
protected void AddExcel(DataSet ds)
{
DataTable dt = ds.Tables[0];
string fileName = Guid.NewGuid() + ".xls";
Excel.Application excel = new Excel.ApplicationClass();
int rowIndex = 1;
int colIndex = 0;
excel.Application.Workbooks.Add(true);
foreach (DataColumn col in dt.Columns)
{
colIndex++;
excel.Cells[1, colIndex] = col.ColumnName;
}
foreach (DataRow row in dt.Rows)
{
rowIndex++;
colIndex = 0;
for (colIndex = 0; colIndex < dt.Columns.Count; colIndex++)
{
excel.Cells[rowIndex, colIndex + 1] = row[colIndex].ToString();
}
}
excel.Visible = false;
excel.ActiveWorkbook.SaveAs(fileName, Excel.XlFileFormat.xlExcel9795, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
//excel.Save(fileName);
excel.Quit();
excel = null;
GC.Collect();//垃圾回收
}
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle =Excel.XlLineStyle.xlContinuous;
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight =Excel.XlBorderWeight.xlThin;
if(ds.Tables[0].Columns.Count>1){
range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex =Excel.XlColorIndex.xlColorIndexAutomatic;
range.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlContinuous;
range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;
}
if(saveFileName!=""){
try{
workbook.Saved =true;
workbook.SaveCopyAs(saveFileName);
fileSaved=true;
}catch(Exception ex){
fileSaved=false;
MessageBox.Show("导出文件时出错,文件可能正被打开!\n"+ex.Message);
}
}else{
fileSaved=false;
}
xlApp.Quit();
GC.Collect();//强行销毁
if(fileSaved && File.Exists(saveFileName)) System.Diagnostics.Process.Start(saveFileName);
}
[解决办法]
学习~
[解决办法]
简单的就用用这个吧
using Excel; string FPath=@System.Windows.Forms.Application.StartupPath+"\\AAA.xls";Excel.ApplicationClass ex=new Excel.ApplicationClass();ex.Application.Workbooks.Add(FPath);ex.Cells[1,1]="赋值";ex.Visible=true;
[解决办法]
参考
http://blog.csdn.net/zczc198401/archive/2008/02/14/2096089.aspx
http://www.cnblogs.com/Ihaveadream/archive/2008/08/17/1269960.html
[解决办法]
学习