惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Know Your Adversary
Know Your Adversary
云风的 BLOG
云风的 BLOG
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
B
Blog
罗磊的独立博客
宝玉的分享
宝玉的分享
Vercel News
Vercel News
Martin Fowler
Martin Fowler
N
Netflix TechBlog - Medium
P
Proofpoint News Feed
T
Threatpost
Security Latest
Security Latest
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Stack Overflow Blog
Stack Overflow Blog
I
Intezer
P
Privacy International News Feed
D
Docker
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
M
MIT News - Artificial intelligence
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
L
Lohrmann on Cybersecurity
Google DeepMind News
Google DeepMind News
The Last Watchdog
The Last Watchdog
A
Arctic Wolf
IT之家
IT之家
S
SegmentFault 最新的问题
S
Securelist
博客园 - 叶小钗
N
News and Events Feed by Topic
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
Engineering at Meta
Engineering at Meta
Hacker News: Ask HN
Hacker News: Ask HN
博客园 - Franky
GbyAI
GbyAI
AI
AI
Y
Y Combinator Blog
WordPress大学
WordPress大学
Latest news
Latest news
Microsoft Security Blog
Microsoft Security Blog
人人都是产品经理
人人都是产品经理
N
News | PayPal Newsroom
The Cloudflare Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
I
InfoQ

博客园 - 林骄

Android note 2010.08.02 v2 2010.08.03 Android note Android Note 2010.08.02 Android Note 2011.08.01 [Biztalk]问题集 - 林骄 - 博客园 Excel单元格颜色 判断系统版本 ListView && XmlReader How to open a folder with explorer - 林骄 Print Can't open Infragistics help document How to get local machine date format.如何获取本机时间格式 cannot open user default database WebService Tips 中文全半角转换 Windows Service 装机备忘 Db2中的时间
Winform导出Excel
林骄 · 2010-11-02 · via 博客园 - 林骄

  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[11], excelApplication.Cells[11]);
                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,
                    
falsefalse, 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