Winform导出Excel
Microsoft.Office.Interop.Excel.Application excelApplication;
Microsoft.Office.Interop.Excel.Workbooks workBooks;
Microsoft.Office.Interop.Excel.Workbook workBook;
Microsoft.Office.Interop.Excel.Worksheet workSheet;
Microsoft.Office.Interop.Excel.Range range;
object Nothing;
public void Export()
{
try
{
excelApplication = new Microsoft.Office.Interop.Excel.Application();
workBooks = excelApplication.Workbooks;
workBook = workBooks.Add(true);
workSheet = workBook.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;
Nothing = System.Reflection.Missing.Value;
string filePath = "c:\\test.xls";
if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
}
int row, col;
//excelApplication.Cells[1, 1] = "1.0";
range = excelApplication.get_Range(excelApplication.Cells[1, 1], excelApplication.Cells[1, 1]);
range.Value2 = "1.0";
range.ColumnWidth = 20;
range.RowHeight = 20;
range.Font.Bold = true;
range.Font.Size = 8.5;
range.Font.Name = "Arial";
range.Font.ColorIndex = 3;
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlLeft;
range.VerticalAlignment = Microsoft.Office.Interop.Excel.Constants.xlCenter;//XlVAlign.xlVAlignCenter;// 设置单元格垂直居中
range.Interior.ColorIndex = 15;//1 Black, 2 White, 3 Red, 4 Green, 5 Blue, 6 Yellow
range.Borders.LineStyle = 1;//边框 不能带 Weight
range.NumberFormatLocal = "#,##0.00";// 设置单元格格式为货币格式
row = col = 2;
range = excelApplication.get_Range(excelApplication.Cells[row, col], excelApplication.Cells[row, col]);
range.Value2 = DateTime.Now;
range.EntireColumn.AutoFit();
range.NumberFormat = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.FullDateTimePattern;
//range.NumberFormat = "yyyy-MM-dd h:mm:ss tt";
//range.EntireColumn.AutoFit();
//range = workSheet.get_Range("B2", "D4");// 获取多个单元格
//range.Merge(Missing.Value); // 合并单元格
//range.Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick; // // 设置单元格右边框加粗
workSheet.PageSetup.PaperSize = XlPaperSize.xlPaperA4; // 设置页面大小为A4
workSheet.PageSetup.Orientation = XlPageOrientation.xlPortrait; // 设置垂直版面
workSheet.PageSetup.HeaderMargin = 0.0; // 设置页眉边距
workSheet.PageSetup.FooterMargin = 0.0; // 设置页脚边距
workSheet.PageSetup.LeftMargin = excelApplication.InchesToPoints(0.354330708661417); // 设置左边距
workSheet.PageSetup.RightMargin = excelApplication.InchesToPoints(0.354330708661417);// 设置右边距
workSheet.PageSetup.TopMargin = excelApplication.InchesToPoints(0.393700787401575); // 设置上边距
workSheet.PageSetup.BottomMargin = excelApplication.InchesToPoints(0.393700787401575);// 设置下边距
workSheet.PageSetup.CenterHorizontally = true; // 设置水平居中
// 打印文件
//workSheet.PrintOut(Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
workBook.SaveAs(filePath, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Nothing, Nothing,
false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Nothing, Nothing, Nothing, Nothing, Nothing);
MessageBox.Show("Success");
//System.Diagnostics.Process.Start(filePath);
}
catch (Exception)
{
throw;
}
finally
{
ReleaseExcel();
}
}
public void ReleaseExcel()
{
if (excelApplication != null)
{
if (workBooks != null)
{
if (workBook != null)
{
if (workSheet != null)
{
if (range != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
range = null;
}
System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
workSheet = null;
}
workBook.Close(false, Nothing, Nothing);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
workBook = null;
}
workBooks.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(workBooks);
workBooks = null;
}
excelApplication.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApplication);
excelApplication = null;
}
}
Microsoft.Office.Interop.Excel.Workbooks workBooks;
Microsoft.Office.Interop.Excel.Workbook workBook;
Microsoft.Office.Interop.Excel.Worksheet workSheet;
Microsoft.Office.Interop.Excel.Range range;
object Nothing;
public void Export()
{
try
{
excelApplication = new Microsoft.Office.Interop.Excel.Application();
workBooks = excelApplication.Workbooks;
workBook = workBooks.Add(true);
workSheet = workBook.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;
Nothing = System.Reflection.Missing.Value;
string filePath = "c:\\test.xls";
if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
}
int row, col;
//excelApplication.Cells[1, 1] = "1.0";
range = excelApplication.get_Range(excelApplication.Cells[1, 1], excelApplication.Cells[1, 1]);
range.Value2 = "1.0";
range.ColumnWidth = 20;
range.RowHeight = 20;
range.Font.Bold = true;
range.Font.Size = 8.5;
range.Font.Name = "Arial";
range.Font.ColorIndex = 3;
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlLeft;
range.VerticalAlignment = Microsoft.Office.Interop.Excel.Constants.xlCenter;//XlVAlign.xlVAlignCenter;// 设置单元格垂直居中
range.Interior.ColorIndex = 15;//1 Black, 2 White, 3 Red, 4 Green, 5 Blue, 6 Yellow
range.Borders.LineStyle = 1;//边框 不能带 Weight
range.NumberFormatLocal = "#,##0.00";// 设置单元格格式为货币格式
row = col = 2;
range = excelApplication.get_Range(excelApplication.Cells[row, col], excelApplication.Cells[row, col]);
range.Value2 = DateTime.Now;
range.EntireColumn.AutoFit();
range.NumberFormat = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.FullDateTimePattern;
//range.NumberFormat = "yyyy-MM-dd h:mm:ss tt";
//range.EntireColumn.AutoFit();
//range = workSheet.get_Range("B2", "D4");// 获取多个单元格
//range.Merge(Missing.Value); // 合并单元格
//range.Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick; // // 设置单元格右边框加粗
workSheet.PageSetup.PaperSize = XlPaperSize.xlPaperA4; // 设置页面大小为A4
workSheet.PageSetup.Orientation = XlPageOrientation.xlPortrait; // 设置垂直版面
workSheet.PageSetup.HeaderMargin = 0.0; // 设置页眉边距
workSheet.PageSetup.FooterMargin = 0.0; // 设置页脚边距
workSheet.PageSetup.LeftMargin = excelApplication.InchesToPoints(0.354330708661417); // 设置左边距
workSheet.PageSetup.RightMargin = excelApplication.InchesToPoints(0.354330708661417);// 设置右边距
workSheet.PageSetup.TopMargin = excelApplication.InchesToPoints(0.393700787401575); // 设置上边距
workSheet.PageSetup.BottomMargin = excelApplication.InchesToPoints(0.393700787401575);// 设置下边距
workSheet.PageSetup.CenterHorizontally = true; // 设置水平居中
// 打印文件
//workSheet.PrintOut(Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
workBook.SaveAs(filePath, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Nothing, Nothing,
false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Nothing, Nothing, Nothing, Nothing, Nothing);
MessageBox.Show("Success");
//System.Diagnostics.Process.Start(filePath);
}
catch (Exception)
{
throw;
}
finally
{
ReleaseExcel();
}
}
public void ReleaseExcel()
{
if (excelApplication != null)
{
if (workBooks != null)
{
if (workBook != null)
{
if (workSheet != null)
{
if (range != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
range = null;
}
System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
workSheet = null;
}
workBook.Close(false, Nothing, Nothing);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
workBook = null;
}
workBooks.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(workBooks);
workBooks = null;
}
excelApplication.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApplication);
excelApplication = null;
}
}
http://www.cnblogs.com/smjack/archive/2009/02/25/1398257.html Example
http://www.cnblogs.com/wangshuai/archive/2010/03/10/1682811.html 判断版本
http://www.cnblogs.com/cdplayer/archive/2008/05/13/1194901.html Format Example
http://www.cnblogs.com/wangshuai/archive/2010/03/10/1682813.html Format