日期:2014-05-17 浏览次数:20935 次
private static bool ExportExcel()
{
if (saveFileName.IndexOf(":") < 0) return false; //被点了取消
Excel.Application xlApp = new Excel.Application();
object missing = System.Reflection.Missing.Value;
if (xlApp == null)
{
MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel");
return false;
}
Excel.Workbooks workbooks = xlApp.Workbooks;
Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1]; //取得sheet1
Excel.Range range;
string strCaption = saveFileName.Remove(0, saveFileName.LastIndexOf('\\') + 1);
strCaption = strCaption.Remove(strCaption.Length - 4, 4);
long totalCount = dtDataSource.Rows.Count;
long rowRead = 0;
float percent = 0;
range = (Excel.Range)worksheet.get_Range(worksheet.Cells[1, 1],
worksheet.Cells[2, dtDataSource.Columns.Count]); //标题占用前两行