{"id":234,"date":"2012-08-05T19:36:01","date_gmt":"2012-08-05T11:36:01","guid":{"rendered":"http:\/\/fishinbox.tk\/?p=234"},"modified":"2012-08-05T19:36:01","modified_gmt":"2012-08-05T11:36:01","slug":"office-excelexcel","status":"publish","type":"post","link":"https:\/\/zhuangzr.me\/wordpress\/2012\/08\/office-excelexcel\/","title":{"rendered":"\u5fae\u8f6fOffice Excel\u5f00\u53d1\u2014\u2014\u6253\u5f00Excel\u6587\u6863"},"content":{"rendered":"<p>\u4eca\u5929\u5728\u4e00\u4e2aVB\u4ea4\u6d41\u7fa4\u4e0a\u9762\u89c1\u5230\u4eba\u95ee\u600e\u4e48\u6253\u5f00\u4e00\u4e2a\u6709\u5199\u4fdd\u62a4\u7684Excel\u6587\u6863\u800c\u4e0d\u9700\u8981\u7528\u6237\u989d\u5916\u64cd\u4f5c\u3002\u5176\u5b9e\u8fd9\u4e2a\u95ee\u9898\u81ea\u5df1\u67e5\u9605MSDN\u5c31\u5b8c\u5168\u53ef\u4ee5\u89e3\u51b3\u7684\uff0c\u4f46\u662f\u6700\u540e\u4e0d\u5f97\u5df2\u8fd8\u662f\u53d7\u4eba\u4ee5\u9c7c\u4e86\uff0c\u6574\u7406\u5728\u6b64\uff0c\u4e5f\u7b97\u662f\u81ea\u5df1\u7684\u56de\u987e\u5427\u3002<\/p>\n<p>\u672c\u6b21\u63a2\u8ba8\u7684\u80cc\u666f\u662f\u5fae\u8f6fdotNet\u6846\u67b6\uff0c\u4e3b\u8981\u4ee3\u7801\u8bed\u8a00\u662fC#\/CSharp\u3002\u7f16\u5199\u73af\u5883Visual Studio 2010\uff0c\u6d4b\u8bd5\u73af\u5883Windows 7 64 bit &amp; Office 2010 Pro Plus 64bit\u3002\u7406\u8bba\u4e0a\u652f\u6301\u7684Excel\u7248\u672c\u6700\u4f4e\u5230Excel 2003\uff0c\u5728\u4f4e\u7684\u7248\u672c\u5c31\u4e0d\u6562\u4fdd\u8bc1\u4e86\u3002<\/p>\n<p>\u6240\u6709Excel\u76f8\u5173\u64cd\u4f5c\u4f7f\u7528\u5fae\u8f6fMicrosoft.Office.Interop.Excel\u547d\u540d\u7a7a\u95f4\uff0c\u6211\u4eec\u8fd9\u91cc\u5148\u505a\u5f15\u7528\uff1a<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nusing Excel = Microsoft.Office.Interop.Excel;\n<\/pre>\n<p>\u8a00\u5f52\u6b63\u4f20\u3002<\/p>\n<h2>\u53d6\u5f97\u4e0eExcel\u7a0b\u5e8f\u7684\u901a\u4fe1<\/h2>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nExcel.Application excel = new Excel.Application();\nexcel.Visible = false;\n<\/pre>\n<p>\u58f0\u660eExcel\u7a0b\u5e8f\u5b9e\u4f8b\uff0c\u5e76\u4e14\u4e0d\u663e\u793aExcel\u7684\u754c\u9762\u3002<\/p>\n<h3>Workbook.Open()\u65b9\u6cd5<\/h3>\n<p>\u6253\u5f00Excel\u6587\u6863\u4f7f\u7528\u4e0b\u5217\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nString filename=&quot;filename&quot;; \/\/\u6587\u4ef6\u8def\u5f84\nExcel.Workbook workbook = excel.Workbooks.Open(filename);\n<\/pre>\n<p>\u5176\u4e2dWorkbook.Open\u7684\u539f\u578b\u662f<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nWorkbook Open(\n\tstring Filename,\n\tObject UpdateLinks,\n\tObject ReadOnly,\n\tObject Format,\n\tObject Password,\n\tObject WriteResPassword,\n\tObject IgnoreReadOnlyRecommended,\n\tObject Origin,\n\tObject Delimiter,\n\tObject Editable,\n\tObject Notify,\n\tObject Converter,\n\tObject AddToMru,\n\tObject Local,\n\tObject CorruptLoad\n)\n<\/pre>\n<p><strong>Filename<\/strong><br \/>\n\u7c7b\u578b: System.String<br \/>\n\u5fc5\u9700\u5b57\u7b26\u4e32\uff0c\u7ed9\u51fa\u6240\u6253\u5f00\u5de5\u4f5c\u7c3f\u7684\u8def\u5f84\u3002<\/p>\n<p><strong>UpdateLinks<\/strong><br \/>\n\u7c7b\u578b: System.Object<br \/>\n\u53ef\u9009\u5bf9\u8c61\u3002 Specifies the way links in the file are updated. If this argument is omitted, the user is prompted to specify how links will be updated. Otherwise, this argument is one of the values listed in the following table.<br \/>\nIf Microsoft Excel is opening a file in the WKS, WK1, or WK3 format and the UpdateLinks argument is 2, Microsoft Excel generates charts from the graphs attached to the file. If the argument is 0, no charts are created.<\/p>\n<p><strong>ReadOnly<\/strong><br \/>\n\u7c7b\u578b: System.Object<br \/>\n\u53ef\u9009\u5bf9\u8c61\u3002\u8bbe\u4e3atrue\u5219\u4ee5\u53ea\u8bfb\u6a21\u5f0f\u6253\u5f00\u5de5\u4f5c\u7c3f\u3002<\/p>\n<p><strong>Format<\/strong><br \/>\n\u7c7b\u578b: System.Object<br \/>\n\u53ef\u9009\u5bf9\u8c61\u3002 If Microsoft Excel is opening a text file, this argument specifies the delimiter character, as shown in the following table. If this argument is omitted, the current delimiter is used.<\/p>\n<p><strong>Password<\/strong><br \/>\n\u7c7b\u578b: System.Object<br \/>\n\u53ef\u9009\u5bf9\u8c61\u3002 \u6253\u5f00\u53d7\u4fdd\u62a4\u5de5\u4f5c\u7c3f\u6240\u9700\u7684\u5bc6\u7801\u5b57\u7b26\u4e32\u3002 \u5982\u679c\u8be5\u53c2\u6570\u88ab\u5ffd\u7565\u800c\u5de5\u4f5c\u7c3f\u662f\u53d7\u4fdd\u62a4\u7684\uff0c\u7528\u6237\u5c06\u4f1a\u88ab\u8981\u6c42\u63d0\u4f9b\u5bc6\u7801\u3002<\/p>\n<p><strong>WriteResPassword<\/strong><br \/>\n\u7c7b\u578b: System.Object<br \/>\n\u53ef\u9009\u5bf9\u8c61\u3002 \u4fee\u6539\u5199\u4fdd\u62a4\u5de5\u4f5c\u7c3f\u6240\u9700\u7684\u5199\u4fdd\u62a4\u5bc6\u7801\u5b57\u7b26\u4e32\u3002 \u5982\u679c\u8be5\u53c2\u6570\u88ab\u5ffd\u7565\u800c\u5de5\u4f5c\u7c3f\u662f\u5199\u4fdd\u62a4\u7684\uff0c\u7528\u6237\u5c06\u4f1a\u88ab\u8981\u6c42\u63d0\u4f9b\u5199\u4fdd\u62a4\u5bc6\u7801\u3002<\/p>\n<p><strong>IgnoreReadOnlyRecommended<\/strong><br \/>\n\u7c7b\u578b: System.Object<br \/>\n\u53ef\u9009\u5bf9\u8c61\u3002\u8bbe\u7f6e\u4e3atrue\u6765\u907f\u514d\u663e\u793a\u63a8\u8350\u53ea\u8bfb\u6a21\u5f0f\u6253\u5f00\u6d88\u606f\u3002<\/p>\n<p><strong>Origin<\/strong><br \/>\n\u7c7b\u578b: System.Object<br \/>\n\u53ef\u9009\u5bf9\u8c61\u3002 If the file is a text file, this argument indicates where it originated (so that code pages and Carriage Return\/Line Feed (CR\/LF) can be mapped correctly). Can be one of the following XlPlatform constants: xlMacintosh, xlWindows, or xlMSDOS. If this argument is omitted, the current operating system is used.<\/p>\n<p><strong>Delimiter<\/strong><br \/>\n\u7c7b\u578b: System.Object<br \/>\n\u53ef\u9009\u5bf9\u8c61\u3002 \u5982\u679c\u76ee\u6807\u6587\u4ef6\u662f\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\u4e14Format\u53c2\u6570\u8bbe\u7f6e\u4e3a6, \u5219\u672c\u53c2\u6570\u662f\u4e00\u4e2a\u7528\u6765\u786e\u5b9a\u5206\u9694\u7b26\u7684String\u5bf9\u8c61\u3002 \u53ea\u6709\u5b57\u7b26\u4e32\u9996\u4f4d\u7684\u5b57\u7b26\u4f1a\u88ab\u7528\u4f5c\u4e3a\u5206\u9694\u7b26\u3002<\/p>\n<p><strong>Editable<\/strong><br \/>\n\u7c7b\u578b: System.Object<br \/>\n\u53ef\u9009\u5bf9\u8c61\u3002 If the file is a Microsoft Excel 4.0 add-in, this argument is True to open the add-in so that it\u2019s a visible window. If this argument is False or omitted, the add-in is opened as hidden, and it cannot be unhidden. This option doesn&#8217;t apply to add-ins created in Microsoft Excel 5.0 or later. If the file is an Excel template, use True to open the specified template for editing or False to open a new workbook based on the specified template. The default value is False.<\/p>\n<p><strong>Notify<\/strong><br \/>\n\u7c7b\u578b: System.Object<br \/>\n\u53ef\u9009\u5bf9\u8c61\u3002 If the file cannot be opened in read\/write mode, this argument is True to add the file to the file notification list. Microsoft Excel will open the file as read-only, poll the file notification list, and then notify the user when the file becomes available. If this argument is False or omitted, no notification is requested, and any attempts to open an unavailable file will fail.<\/p>\n<p><strong>Converter<\/strong><br \/>\n\u7c7b\u578b: System.Object<br \/>\n\u53ef\u9009\u5bf9\u8c61\u3002 The index of the first file converter to try when opening the file. The specified file converter is tried first; if this converter doesn\u2019t recognize the file, all other converters are tried. The converter index consists of the row numbers of the converters returned by the FileConverters property.<\/p>\n<p><strong>AddToMru<\/strong><br \/>\n\u7c7b\u578b: System.Object<br \/>\n\u53ef\u9009\u5bf9\u8c61\u3002 True to add this workbook to the list of recently used files. The default value is False.<\/p>\n<p><strong>Local<\/strong><br \/>\n\u7c7b\u578b: System.Object<br \/>\n\u53ef\u9009\u5bf9\u8c61\u3002 True saves files against the language of Microsoft Excel (including control panel settings). False (default) saves files against the language of Visual Basic for Applications (VBA) (which is typically U.S. English unless the VBA project where Workbooks.Open is run from is an old internationalized XL5\/95 VBA project).<\/p>\n<p><strong>CorruptLoad<\/strong><br \/>\n\u7c7b\u578b: System.Object<br \/>\n\u53ef\u9009\u5bf9\u8c61\u3002 Can be one of the following constants: xlNormalLoad, xlRepairFile, and xlExtractData. The default behavior if no value is specified is usually normal, but may be safe load or data recovery if Excel has already attempted to open the file. The first attempt is normal. If Excel stops operating while opening the file, the second attempt is safe load. If Excel again stops operating, the next attempt is data recovery.<\/p>\n<p><strong>\u53ef\u9009\u53c2\u6570\u7684\u5ffd\u7565<\/strong><br \/>\n\u6211\u4eec\u6ce8\u610f\u5230\uff0c\u53ea\u6709\u7b2c\u4e00\u4e2a\u53c2\u6570filename\u662f\u5fc5\u987b\u53c2\u6570\uff0c\u5176\u4ed6\u7684\u90fd\u662f\u53ef\u9009\u53c2\u6570\u3002 \u4e0d\u60f3\u660e\u786e\u7684\u53ef\u9009\u53c2\u6570\u53ef\u4ee5\u4f7f\u7528Type.Missing\uff08C#\uff09\u6216missing\uff08VB\uff09\u4ee3\u66ff\u3002<\/p>\n<h2>\u4e0eExcel\u4ea4\u4e92<\/h2>\n<p>\u6211\u4eec\u8fd9\u91cc\u53ea\u505a\u6700\u57fa\u7840\u7684\u4ea4\u4e92\u6f14\u793a\uff0c\u8be6\u60c5\u8bf7\u67e5\u6280\u672f\u6587\u6863\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nExcel.Worksheet worksheet = workbook.ActiveSheet;\nRange range = worksheet.get_Range(&quot;A1&quot;,Type.Missing);\n\/\/get_Range(cell1,cell2)\u53ef\u4ee5\u63a5\u53d72\u4e2a\u53c2\u6570\uff0c\u53ef\u4ee5\u662fExcel A1 Style\u7684\u5b57\u7b26\u4e32\u4ee3\u8868\u76ee\u6807\u5355\u5143\u683c\u533a\u57df\u7684\u5de6\u4e0a\u53ca\u53f3\u4e0b\u5355\u5143\u683c\n\/\/range.Value\u5c06\u8bbe\u7f6e\u6216\u8fd4\u56de\u6307\u5b9a\u533a\u57df\u7684\u503c\n\/\/range.Value2\u5c06\u8bbe\u7f6e\u6216\u8fd4\u56de\u6307\u5b9a\u5355\u5143\u683c\u7684\u503c\n&lt;\/pre&gt;\n&lt;h1&gt;\u4e0eExcel\u901a\u4fe1\u7684\u7ed3\u675f&lt;\/h1&gt;\n\u6211\u4eec\u4f7f\u7528_Application\u63a5\u53e3\u4e0b\u7684Quit()\u65b9\u6cd5\uff0c\u5fc5\u987b\u5148\u8c03\u7528Quit\u65b9\u6cd5\uff0cExcel\u8fdb\u7a0b\u624d\u80fd\u88ab\u91ca\u653e\uff1a\n&lt;pre lang=&quot;csharp&quot;&gt;\nexcel.Quit();\nexcel=null;\n<\/pre>\n<h2>\u793a\u4f8b<\/h2>\n<p>\u6211\u4eec\u73b0\u5728\u6709\u4e00\u4e2a\u540d\u4e3atest.xlsx\u7684Excel\u6587\u6863\uff0c\u5206\u522b\u6709\u6587\u6863\u4fdd\u62a4\u5bc6\u7801&#8221;pswd1&#8243;\u4ee5\u53ca\u6587\u6863\u5199\u4fdd\u62a4\u5bc6\u7801&#8221;pswd2&#8243;\u3002\u6211\u4eec\u4e0d\u5e0c\u671b\u4fee\u6539Excel\u6587\u6863\u5185\u5bb9\uff0c\u53ea\u9700\u8981\u8bfb\u53d6A1\u5355\u5143\u683c\u7684\u5185\u5bb9\u5e76\u4e14\u663e\u793a\u51fa\u6765\uff0c\u5e76\u4e14\u4e0d\u9700\u8981\u7528\u6237\u7684\u989d\u5916\u64cd\u4f5c\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n    String passwd = &quot;pswd1&quot;;\n    String WRPasswd = &quot;pswd2&quot;;\n    Excel.Application excel = new Excel.Application();\n    Excel.Workbook workbook = excel.Workbooks.Open(@&quot;E:ShowCaseExcel1test.xlsx&quot;, Type.Missing, true, Type.Missing,passwd);\n    Excel.Worksheet worksheet = workbook.ActiveSheet;\n    String content=worksheet.get_Range(&quot;A1&quot;, Type.Missing).Value2;\n    excel.Quit();\n    excel = null;\n    MessageBox.Show(content);\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u5728\u4e00\u4e2aVB\u4ea4\u6d41\u7fa4\u4e0a\u9762\u89c1\u5230\u4eba\u95ee\u600e\u4e48\u6253\u5f00\u4e00\u4e2a\u6709\u5199\u4fdd\u62a4\u7684Excel\u6587\u6863\u800c\u4e0d\u9700\u8981\u7528\u6237\u989d\u5916\u64cd\u4f5c\u3002\u5176\u5b9e\u8fd9\u4e2a\u95ee\u9898\u81ea\u5df1\u67e5\u9605MSDN\u5c31\u5b8c\u5168\u53ef\u4ee5\u89e3\u51b3\u7684\uff0c\u4f46\u662f\u6700\u540e\u4e0d\u5f97\u5df2\u8fd8\u662f\u53d7\u4eba\u4ee5\u9c7c\u4e86\uff0c\u6574\u7406\u5728\u6b64\uff0c\u4e5f\u7b97\u662f\u81ea\u5df1\u7684\u56de\u987e\u5427\u3002 \u672c\u6b21\u63a2\u8ba8\u7684\u80cc\u666f\u662f\u5fae\u8f6fdotNet\u6846\u67b6\uff0c\u4e3b\u8981\u4ee3\u7801\u8bed\u8a00\u662fC#\/CSharp\u3002\u7f16\u5199\u73af\u5883Visual Studio 2010\uff0c\u6d4b\u8bd5\u73af\u5883Windows 7 64 bit &amp; Office 2010 Pro Plus 64bit\u3002\u7406\u8bba\u4e0a\u652f\u6301\u7684Excel\u7248\u672c\u6700\u4f4e\u5230Excel 2003\uff0c\u5728\u4f4e\u7684\u7248\u672c\u5c31\u4e0d\u6562\u4fdd\u8bc1\u4e86\u3002 \u6240\u6709Excel\u76f8\u5173\u64cd\u4f5c\u4f7f\u7528\u5fae\u8f6fMicrosoft.Office.Interop.Excel\u547d\u540d\u7a7a\u95f4\uff0c\u6211\u4eec\u8fd9\u91cc\u5148\u505a\u5f15\u7528\uff1a using Excel = Microsoft.Office.Interop.Excel; \u8a00\u5f52\u6b63\u4f20\u3002 \u53d6\u5f97\u4e0eExcel\u7a0b\u5e8f\u7684\u901a\u4fe1 Excel.Application excel = new Excel.Application(); excel.Visible = false; \u58f0\u660eExcel\u7a0b\u5e8f\u5b9e\u4f8b\uff0c\u5e76\u4e14\u4e0d\u663e\u793aExcel\u7684\u754c\u9762\u3002 Workbook.Open()\u65b9\u6cd5 \u6253\u5f00Excel\u6587\u6863\u4f7f\u7528\u4e0b\u5217\u65b9\u6cd5\uff1a String filename=&quot;filename&quot;; \/\/\u6587\u4ef6\u8def\u5f84 Excel.Workbook workbook = excel.Workbooks.Open(filename); \u5176\u4e2dWorkbook.Open\u7684\u539f\u578b\u662f Workbook Open( string Filename, Object UpdateLinks, Object ReadOnly, Object Format, Object Password, Object WriteResPassword, Object &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/zhuangzr.me\/wordpress\/2012\/08\/office-excelexcel\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;\u5fae\u8f6fOffice Excel\u5f00\u53d1\u2014\u2014\u6253\u5f00Excel\u6587\u6863&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[29,31,42,50],"class_list":["post-234","post","type-post","status-publish","format-standard","hentry","category-csharp-programming","tag-csharp","tag-excel","tag-office","tag-vsto"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/posts\/234","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/comments?post=234"}],"version-history":[{"count":0,"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/posts\/234\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/media?parent=234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/categories?post=234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuangzr.me\/wordpress\/wp-json\/wp\/v2\/tags?post=234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}