ApplicationClass excel = new ApplicationClass(); excel.Visible = true; //激活Excel Workbook wBook = excel.Workbooks.Add(true); Worksheet wSheet = (Worksheet)wBook.ActiveSheet; //全表自动列宽 wSheet.Cells.Select();//先选择 wSheet.Cells.Columns.AutoFit();//后操作,自动列宽 wSheet.Cells.NumberFormat = "@";//后操作,设置单元格为广西格式
wSheet.Name = "联系人信息"; excel.Cells[1, 1] = "会员卡号"; excel.Cells[1, 2] = "姓名"; excel.Cells[1, 3] = "年龄"; excel.Cells[1, 4] = "电话"; excel.Cells[1, 5] = "地址"; excel.Cells[1, 6] = "性别";
List<AddrLibInfo> results = GetAllInfos(); int index = 2; foreach (AddrLibInfo info in results) { if (info.Type == "0") { excel.Cells[index, 1] = info.BUYERID.ToString(); excel.Cells[index, 2] = info.NAME.ToString(); excel.Cells[index, 3] = info.AGE.ToString(); excel.Cells[index, 4] = info.TEL.ToString(); excel.Cells[index, 5] = info.ADDRESS.ToString(); excel.Cells[index, 6] = info.GENDER.ToString(); index++; } }
//设置禁止弹出保存和覆盖的询问提示框 excel.DisplayAlerts = false; excel.AlertBeforeOverwriting = false; //保存工作薄 //wBook.Save(); //每次保存激活的表,这样才能多次操作保存不同的Excel表,默认保存位置是在”我的文档" excel.Cells.Font.Size = 12; excel.Cells.Font.Bold = false; //wSheet.get_Range(excel.Cells[1, 3], excel.Cells[1, 3]).Font.Size = 24; wSheet.get_Range(excel.Cells[1, 1], excel.Cells[1, 6]).Font.Bold = true; //wSheet.get_Range(excel.Cells[3, 1], excel.Cells[3, 1]).Font.ColorIndex = 3;//此处设为红色,不能用Font.Color来设置颜色 excel.ActiveWorkbook.SaveCopyAs(path + "\\联系人信息.xls"); excel.Quit();
转载于:https://www.cnblogs.com/nanshouyong326/archive/2009/10/20/1586715.html
相关资源:防止GridView导出到excel中自动转成科学计数法