Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for excel2any.cs

(view source code of excel2any.cs as plain text)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Excel = Microsoft.Office.Interop.Excel;
  5.  
  6.  
  7. namespace RobvanderWoude
  8. {
  9. 	class Excel2Any
  10. 	{
  11. 		static string progver = "1.00";
  12.  
  13.  
  14. 		static int Main( string[] args )
  15. 		{
  16. 			#region Initialize Variables
  17.  
  18. 			bool overwrite = false;
  19. 			string inputfilespec = null;
  20. 			string outputfilespec = null;
  21. 			string inputfolder;
  22. 			string outputfolder;
  23. 			string inputfile;
  24. 			string outputfile;
  25. 			string inputfilename;
  26. 			string outputfilename;
  27. 			string inputfileext;
  28. 			string outputfileext = null;
  29. 			int inputformat = -1;
  30. 			int outputformat = -1;
  31.  
  32. 			#endregion Initialize Variables
  33.  
  34.  
  35. 			#region Command Line Parsing
  36.  
  37. 			if ( args.Length == 0 || ( args.Length == 1 && args[0].ToUpper( ) != "/T" ) )
  38. 			{
  39. 				return ShowHelp( );
  40. 			}
  41. 			foreach ( string arg in args )
  42. 			{
  43. 				if ( arg[0] == '/' )
  44. 				{
  45. 					if ( arg.ToString( ).ToUpper( ) == "/O" )
  46. 					{
  47. 						if ( overwrite )
  48. 						{
  49. 							return ShowHelp( "Duplicate command line switch /O" );
  50. 						}
  51. 						overwrite = true;
  52. 						break;
  53. 					}
  54. 					else if ( arg.ToString( ).ToUpper( ) == "/T" )
  55. 					{
  56. 						ListFormats( );
  57. 						return 0;
  58. 					}
  59. 					else if ( arg.Length > 3 && arg.Substring( 0, 3 ).ToUpper( ) == "/T:" )
  60. 					{
  61. 						if ( outputformat != -1 )
  62. 						{
  63. 							return ShowHelp( "Duplicate command line switch /T" );
  64. 						}
  65. 						outputformat = GetFormat( arg.Substring( 3 ) );
  66. 						if ( outputformat == -1 )
  67. 						{
  68. 							return ShowHelp( "Output file type not recognized, use /T to list all available types" );
  69. 						}
  70. 					}
  71. 					else
  72. 					{
  73. 						return ShowHelp( "Invalid command line switch {0}", arg );
  74. 					}
  75. 				}
  76. 				else
  77. 				{
  78. 					if ( String.IsNullOrEmpty( inputfilespec ) )
  79. 					{
  80. 						inputfilespec = arg;
  81. 					}
  82. 					else if ( String.IsNullOrEmpty( outputfilespec ) )
  83. 					{
  84. 						outputfilespec = arg;
  85. 					}
  86. 					else
  87. 					{
  88. 						return ShowHelp( "Invalid command line argument \"{0}\"", arg );
  89. 					}
  90. 				}
  91. 			}
  92.  
  93. 			#endregion Command Line Parsing
  94.  
  95.  
  96. 			#region Command Line Validation
  97.  
  98. 			// Validate input filespec
  99. 			if ( String.IsNullOrEmpty( inputfilespec ) )
  100. 			{
  101. 				return ShowHelp( "Please specify an input file" );
  102. 			}
  103. 			switch ( ValidateFilespec( inputfilespec ) )
  104. 			{
  105. 				case -1:
  106. 					if ( inputfilespec.IndexOf( '*' ) == -1 )
  107. 					{
  108. 						return ShowHelp( "Parent folder of input file not found" );
  109. 					}
  110. 					else
  111. 					{
  112. 						return ShowHelp( "Parent folder of input files not found" );
  113. 					}
  114. 				case 0:
  115. 					if ( inputfilespec.IndexOf( '*' ) == -1 )
  116. 					{
  117. 						return ShowHelp( "Input file not found" );
  118. 					}
  119. 					else
  120. 					{
  121. 						return ShowHelp( "No matching input files found" );
  122. 					}
  123. 				case 1:
  124. 					break;
  125. 				default:
  126. 					if ( !String.IsNullOrEmpty( outputfilespec ) && Path.GetFileNameWithoutExtension( outputfilespec ) != "*" )
  127. 					{
  128. 						return ShowHelp( "When using wildcards in the input file names,\n\tyou must use wildcard \"*\" for the output file names, if specified" );
  129. 					}
  130. 					break;
  131. 			}
  132. 			inputfolder = Directory.GetParent( inputfilespec ).FullName;
  133. 			inputfile = Path.GetFileName( inputfilespec );
  134. 			inputfilename = Path.GetFileNameWithoutExtension( inputfilespec );
  135. 			inputfileext = Path.GetExtension( inputfilespec );
  136. 			inputformat = GetFormatByExtension( inputfilespec );
  137.  
  138. 			// Validate or build output filespec
  139. 			if ( String.IsNullOrEmpty( outputfilespec ) )
  140. 			{
  141. 				if ( outputformat == -1 )
  142. 				{
  143. 					return ShowHelp( "Please specify output file(s) and/or valid output file type" );
  144. 				}
  145. 				outputfolder = inputfolder;
  146. 				outputfile = "*";
  147. 				outputfilename = "*";
  148. 				outputfilespec = Path.Combine( outputfolder, outputfile );
  149. 				// Extension will be default extension based on file type
  150. 			}
  151. 			else
  152. 			{
  153. 				outputfile = Path.GetFileName( outputfilespec );
  154. 				outputfilename = Path.GetFileNameWithoutExtension( outputfilespec );
  155. 				outputfileext = Path.GetExtension( outputfilespec );
  156. 				if ( outputfilespec.IndexOf( '\\' ) == -1 ) // e.g. "*.xps" or "filename.pdf"
  157. 				{
  158. 					outputfolder = inputfolder;
  159. 					outputfilespec = Path.Combine( outputfolder, outputfile );
  160. 				}
  161. 				outputfolder = Directory.GetParent( outputfilespec ).FullName;
  162. 				if ( ValidateFilespec( outputfilespec ) == -1 )
  163. 				{
  164. 					if ( outputfilespec.IndexOf( '*' ) == -1 )
  165. 					{
  166. 						return ShowHelp( "Parent folder for output file not found" );
  167. 					}
  168. 					else
  169. 					{
  170. 						return ShowHelp( "Parent folder for output files not found" );
  171. 					}
  172. 				}
  173. 			}
  174. 			if ( outputformat == -1 )
  175. 			{
  176. 				outputformat = GetFormatByExtension( outputfilespec );
  177. 			}
  178.  
  179. 			// Input and output file types should be different
  180. 			if ( inputformat == outputformat )
  181. 			{
  182. 				return ShowHelp( "Input and output file types should be different" );
  183. 			}
  184. 			// Input and output extensions should be different
  185. 			if ( inputfileext == outputfileext )
  186. 			{
  187. 				return ShowHelp( "Input and output file extensions should be different" );
  188. 			}
  189.  
  190. 			#endregion Command Line Validation
  191.  
  192.  
  193. 			#region Iterate File List and Convert Each File
  194.  
  195. 			int rc = 0;
  196.  
  197. 			foreach ( string file in Directory.GetFiles( inputfolder, inputfile ) )
  198. 			{
  199. 				if ( Path.GetExtension( file ) == inputfileext ) // prevent including *.docx when *.doc is specified
  200. 				{
  201. 					string output;
  202. 					if ( inputfilename.IndexOf( '*' ) > -1 )
  203. 					{
  204. 						output = Path.Combine( outputfolder, Path.GetFileNameWithoutExtension( file ) + outputfileext );
  205. 					}
  206. 					else
  207. 					{
  208. 						output = outputfilespec;
  209. 					}
  210. 					if ( File.Exists( output ) && !overwrite )
  211. 					{
  212. 						if ( inputfilename.IndexOf( '*' ) > -1 )
  213. 						{
  214. 							Console.WriteLine( "Skipped \"{0}\" because \"{1}\" already exists", Path.GetFileName( file ), Path.GetFileName( output ) );
  215. 						}
  216. 						else
  217. 						{
  218. 							return ShowHelp( "Output file \"{0}\" already exists, use /O to silently overwrite existing files", Path.GetFileName( output ) );
  219. 						}
  220. 					}
  221. 					else
  222. 					{
  223. 						Console.Write( "Converting \"{0}\" . . . ", Path.GetFileName( file ) );
  224. 						if ( ExcelConvert( file, output, outputformat ) )
  225. 						{
  226. 							Console.WriteLine( "Success" );
  227. 						}
  228. 						else
  229. 						{
  230. 							Console.WriteLine( "Failed" );
  231. 							rc += 1;
  232. 						}
  233. 					}
  234. 				}
  235. 			}
  236.  
  237. 			#endregion Iterate File List and Convert Each File
  238.  
  239.  
  240. 			return rc;
  241. 		}
  242.  
  243.  
  244. 		// Opens the specified spreadsheet and saves or exports it with the specified file name and file type
  245. 		static bool ExcelConvert( string inputpath, string outputpath, int outputtype )
  246. 		{
  247. 			bool error = true;
  248. 			string outputname = Path.GetFileNameWithoutExtension( outputpath );
  249. 			if ( outputname == "*" )
  250. 			{
  251. 				outputpath = Path.Combine( Directory.GetParent( outputpath ).FullName, Path.GetFileNameWithoutExtension( inputpath ) );
  252. 			}
  253. 			try
  254. 			{
  255. 				Excel.Application excelapp = new Excel.Application( );
  256. 				excelapp.Visible = false;
  257. 				excelapp.Visible = true;
  258. 				Excel.Workbook excelworkbook = excelapp.Workbooks.Open( inputpath );
  259. 				try
  260. 				{
  261. 					// export as pdf or xps
  262. 					Excel.XlFixedFormatType type = (Excel.XlFixedFormatType) outputtype;
  263. 					excelworkbook.ExportAsFixedFormat( type, outputpath );
  264. 					error = false;
  265. 				}
  266. 				catch
  267. 				{
  268. 					try
  269. 					{
  270. 						// save as ...
  271. 						Excel.XlFileFormat type = (Excel.XlFileFormat) outputtype;
  272. 						excelworkbook.SaveAs( outputpath, type );
  273. 						error = false;
  274. 					}
  275. 					catch { }
  276. 				}
  277. 				excelworkbook.Close( );
  278. 				excelapp.Quit( );
  279. 			}
  280. 			catch { }
  281. 			return !error;
  282. 		}
  283.  
  284.  
  285. 		// Returns the numeric representation of the Excel file or export format for the specified extension
  286. 		static int GetFormatByExtension( string file )
  287. 		{
  288. 			string ext = Path.GetExtension( file ).ToLower( ).Substring( 1 );
  289. 			Dictionary<string, int> knownexcelexts = new Dictionary<string, int>( );
  290. 			knownexcelexts["csv"] = (int) Excel.XlFileFormat.xlCSV;
  291. 			knownexcelexts["htm"] = (int) Excel.XlFileFormat.xlHtml;
  292. 			knownexcelexts["html"] = (int) Excel.XlFileFormat.xlHtml;
  293. 			knownexcelexts["ods"] = (int) Excel.XlFileFormat.xlOpenDocumentSpreadsheet;
  294. 			knownexcelexts["pdf"] = (int) Excel.XlFixedFormatType.xlTypePDF;
  295. 			knownexcelexts["txt"] = (int) Excel.XlFileFormat.xlTextWindows;
  296. 			knownexcelexts["xls"] = (int) Excel.XlFileFormat.xlWorkbookDefault;
  297. 			knownexcelexts["xlsm"] = (int) Excel.XlFileFormat.xlOpenXMLWorkbookMacroEnabled;
  298. 			knownexcelexts["xlsx"] = (int) Excel.XlFileFormat.xlOpenXMLWorkbook;
  299. 			knownexcelexts["xps"] = (int) Excel.XlFixedFormatType.xlTypeXPS;
  300. 			if ( knownexcelexts.ContainsKey( ext ) )
  301. 			{
  302. 				return knownexcelexts[ext];
  303. 			}
  304. 			return -1;
  305. 		}
  306.  
  307.  
  308. 		// Returns the numeric representation for the specified Excel file or export format
  309. 		static int GetFormat( string format )
  310. 		{
  311. 			// test for numeric fomat (type number)
  312. 			try { return (int) (Excel.XlFileFormat) Convert.ToInt32( format ); }
  313. 			catch { }
  314. 			// test for numeric fomat (pdf/xps)
  315. 			try { return (int) (Excel.XlFixedFormatType) Convert.ToInt32( format ); }
  316. 			catch { }
  317. 			// test for string format (type name)
  318. 			for ( int i = 0; i < 64; i++ )
  319. 			{
  320. 				// all types except pdf/xps
  321. 				try
  322. 				{
  323. 					if ( ( (Excel.XlFileFormat) i ).ToString( ) != i.ToString( ) )
  324. 					{
  325. 						string type = ( (Excel.XlFileFormat) i ).ToString( );
  326. 						if ( format.ToUpper( ) == type.ToUpper( ) )
  327. 						{
  328. 							return (int) (Excel.XlFileFormat) i;
  329. 						}
  330. 					}
  331. 				}
  332. 				catch { }
  333. 				// pdf/xps
  334. 				try
  335. 				{
  336. 					if ( ( (Excel.XlFixedFormatType) i ).ToString( ) != i.ToString( ) )
  337. 					{
  338. 						string type = ( (Excel.XlFixedFormatType) i ).ToString( );
  339. 						if ( format.ToUpper( ) == type.ToUpper( ) )
  340. 						{
  341. 							return (int) (Excel.XlFixedFormatType) i;
  342. 						}
  343. 					}
  344. 				}
  345. 				catch { }
  346. 			}
  347. 			// return -1 if format not valid
  348. 			return -1;
  349. 		}
  350.  
  351.  
  352. 		// Lists all available file formats as string (name) and number
  353. 		static void ListFormats( )
  354. 		{
  355. 			int maxlen = 0;
  356. 			for ( int i = 0; i < 64; i++ )
  357. 			{
  358. 				if ( ( (Excel.XlFileFormat) i ).ToString( ).Length > maxlen )
  359. 				{
  360. 					maxlen = ( (Excel.XlFileFormat) i ).ToString( ).Length;
  361. 				}
  362. 				if ( ( (Excel.XlFixedFormatType) i ).ToString( ).Length > maxlen )
  363. 				{
  364. 					maxlen = ( (Excel.XlFixedFormatType) i ).ToString( ).Length;
  365. 				}
  366. 			}
  367. 			ConsoleColor bgblue = ConsoleColor.DarkBlue;
  368. 			ConsoleColor bgdefault = Console.BackgroundColor;
  369. 			Console.ForegroundColor = ConsoleColor.White;
  370. 			Console.WriteLine( String.Format( "{0,-" + maxlen + "}  {1}", "File Type", "Number" ) );
  371. 			if ( maxlen > 12 )
  372. 			{
  373. 				Console.WriteLine( new String( '=', 12 ) + new String( ' ', maxlen - 12 + 2 ) + new String( '=', 6 ) );
  374. 			}
  375. 			else
  376. 			{
  377. 				Console.WriteLine( new String( '=', maxlen ) + "  " + new String( '=', 6 ) );
  378. 			}
  379. 			int linenum = 0;
  380. 			for ( int i = 0; i < 64; i++ )
  381. 			{
  382. 				if ( ( (Excel.XlFileFormat) i ).ToString( ) != i.ToString( ) )
  383. 				{
  384. 					if ( linenum % 2 == 1 )
  385. 					{
  386. 						Console.BackgroundColor = bgblue;
  387. 					}
  388. 					Console.Write( String.Format( "{0,-" + maxlen + "}  {1,4}  ", (Excel.XlFileFormat) i, i ) );
  389. 					if ( linenum % 2 == 1 )
  390. 					{
  391. 						Console.BackgroundColor = bgdefault;
  392. 					}
  393. 					Console.WriteLine( );
  394. 					linenum += 1;
  395. 				}
  396. 				if ( ( (Excel.XlFixedFormatType) i ).ToString( ) != i.ToString( ) )
  397. 				{
  398. 					if ( linenum % 2 == 1 )
  399. 					{
  400. 						Console.BackgroundColor = bgblue;
  401. 					}
  402. 					Console.Write( String.Format( "{0,-" + maxlen + "}  {1,4}  ", (Excel.XlFixedFormatType) i, i ) );
  403. 					if ( linenum % 2 == 1 )
  404. 					{
  405. 						Console.BackgroundColor = bgdefault;
  406. 					}
  407. 					Console.WriteLine( );
  408. 					linenum += 1;
  409. 				}
  410. 			}
  411. 			Console.ResetColor( );
  412. 		}
  413.  
  414.  
  415. 		// Displays help text
  416. 		static int ShowHelp( params string[] errmsg )
  417. 		{
  418. 			/*
  419. 			Excel2Any,  Version 1.00
  420. 			Open a spreadsheet in Microsoft Excel and save it in "any" (known) format
  421.  
  422. 			Usage:     EXCEL2ANY   "workbook"  [ "outfile" ]  [ options ]
  423.  
  424. 			Where:     "workbook"  Excel document(s) to be converted (wildcard "*" allowed
  425. 			                       in file name, e.g. "name*.xlsx")
  426. 			           "outfile"   output file(s) to be created (wildcard "*" allowed for
  427. 			                       file name, e.g. "*.ods" or "*.pdf")
  428. 			Options:   /O          silently overwrite existing output file(s)
  429. 			                       (default: abort or skip if output file exists)
  430. 			           /T          list available output file types
  431. 			           /T:type     set output file type (required if "outfile" is not
  432. 			                       specified; type may be number or string)
  433.  
  434. 			Notes: [1] This program requires a "regular" (MSI based) Microsoft Excel
  435. 			           (2007 or later) installation, it will fail on an MS Office
  436. 			           "click-to-run" installation.
  437. 			       [2] For Excel 2007, to save as PDF or XPS, this program requires the
  438. 			           "Microsoft Save as PDF or XPS Add-in for 2007 Microsoft Office
  439. 			           programs", available at:
  440. 			           http://www.microsoft.com/en-us/download/details.aspx?id=7
  441. 			       [3] If wildcards are used in the Excel file names, and the output file
  442. 			           path is not specified, /T:type must be used, and the input file
  443. 			           names should not contain dots.
  444. 			       [4] If wildcards are used in the Excel file names, and the output file
  445. 			           path is specified, the output file name must be "*".
  446. 			       [5] If wildcards are used in the Excel file names, and the /O switch
  447. 			           is not used, the program will display an error message in case an
  448. 			           output file already exists, but it will then continue to convert
  449. 			           the next file instead of aborting.
  450. 			       [6] If Excel was already active when this program is started, any
  451. 			           other opened spreadsheet(s) will be left alone, and only the
  452. 			           spreadsheet(s) opened by this program will be closed.
  453.  
  454. 			Examples:  EXCEL2ANY "D:\folder\myfile.xls" *.pdf
  455. 			           will save to "D:\folder\myfile.pdf"
  456.  
  457. 			           EXCEL2ANY "D:\folder\myfile.xlsx" "D:\otherfolder\*.ods"
  458. 			           will save to "D:\otherfolder\myfile.ods"
  459.  
  460. 			           EXCEL2ANY "D:\folder\myfile.xlsx" "D:\elsewhere\newfile.xps"
  461. 			           will save to "D:\elsewhere\newfile.xps"
  462.  
  463. 			           EXCEL2ANY "D:\folder\name*.xlsx" *.html
  464. 			           will save all matching files as HTML to "D:\folder\"
  465. 			           recognized extensions: csv, htm(l), ods, pdf, txt, xls(x), xlsm, xps
  466.  
  467. 			           EXCEL2ANY "D:\folder\*.xlsx" /T:8
  468. 			           like previous example, but more file types available
  469.  
  470. 			           EXCEL2ANY /T
  471. 			           will list all available file types
  472.  
  473. 			Written by Rob van der Woude
  474. 			http://www.robvanderwoude.com
  475. 			*/
  476.  
  477. 			if ( errmsg.Length > 0 )
  478. 			{
  479. 				List<string> errargs = new List<string>( errmsg );
  480. 				errargs.RemoveAt( 0 );
  481. 				Console.Error.WriteLine( );
  482. 				Console.ForegroundColor = ConsoleColor.Red;
  483. 				Console.Error.Write( "ERROR:\t" );
  484. 				Console.ForegroundColor = ConsoleColor.White;
  485. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  486. 				Console.ResetColor( );
  487. 			}
  488.  
  489. 			Console.Error.WriteLine( );
  490.  
  491. 			Console.Error.WriteLine( "Excel2Any,  Version {0}", progver );
  492.  
  493. 			Console.Error.WriteLine( "Open a spreadsheet in Microsoft Excel and save it in \"any\" (known) format" );
  494.  
  495. 			Console.Error.WriteLine( );
  496.  
  497. 			Console.Error.Write( "Usage:     " );
  498. 			Console.ForegroundColor = ConsoleColor.White;
  499. 			Console.Error.WriteLine( "EXCEL2ANY    \"workbook\"  [ \"outfile\" ]  [ options ]" );
  500. 			Console.ResetColor( );
  501.  
  502. 			Console.Error.WriteLine( );
  503.  
  504. 			Console.Error.Write( "Where:     " );
  505. 			Console.ForegroundColor = ConsoleColor.White;
  506. 			Console.Error.Write( "\"workbook\"" );
  507. 			Console.ResetColor( );
  508. 			Console.Error.WriteLine( "  Excel document(s) to be converted (wildcard \"*\" allowed" );
  509.  
  510. 			Console.Error.WriteLine( "                       in file name, e.g. \"name*.xlsx\")" );
  511.  
  512. 			Console.ForegroundColor = ConsoleColor.White;
  513. 			Console.Error.Write( "           \"outfile\"" );
  514. 			Console.ResetColor( );
  515. 			Console.Error.WriteLine( "   output file(s) to be created (wildcard \"*\" allowed for" );
  516.  
  517. 			Console.Error.WriteLine( "                       file name, e.g. \"*.ods\" or \"*.pdf\")" );
  518.  
  519. 			Console.Error.Write( "Options:   " );
  520. 			Console.ForegroundColor = ConsoleColor.White;
  521. 			Console.Error.Write( "/O" );
  522. 			Console.ResetColor( );
  523. 			Console.Error.Write( "          silently " );
  524. 			Console.ForegroundColor = ConsoleColor.White;
  525. 			Console.Error.Write( "O" );
  526. 			Console.ResetColor( );
  527. 			Console.Error.WriteLine( "verwrite existing output file(s)" );
  528.  
  529. 			Console.Error.WriteLine( "                       (default: abort or skip if output file exists)" );
  530.  
  531. 			Console.ForegroundColor = ConsoleColor.White;
  532. 			Console.Error.Write( "           /T" );
  533. 			Console.ResetColor( );
  534. 			Console.Error.Write( "          list available output file " );
  535. 			Console.ForegroundColor = ConsoleColor.White;
  536. 			Console.Error.Write( "T" );
  537. 			Console.ResetColor( );
  538. 			Console.Error.WriteLine( "ypes" );
  539.  
  540.  
  541. 			Console.ForegroundColor = ConsoleColor.White;
  542. 			Console.Error.Write( "           /T:type" );
  543. 			Console.ResetColor( );
  544. 			Console.Error.Write( "     set output file " );
  545. 			Console.ForegroundColor = ConsoleColor.White;
  546. 			Console.Error.Write( "T" );
  547. 			Console.ResetColor( );
  548. 			Console.Error.Write( "ype (required if " );
  549. 			Console.ForegroundColor = ConsoleColor.White;
  550. 			Console.Error.Write( "\"outfile\"" );
  551. 			Console.ResetColor( );
  552. 			Console.Error.WriteLine( " is not" );
  553.  
  554. 			Console.Error.Write( "                       specified; " );
  555. 			Console.ForegroundColor = ConsoleColor.White;
  556. 			Console.Error.Write( "type" );
  557. 			Console.ResetColor( );
  558. 			Console.Error.WriteLine( " may be number or string)" );
  559.  
  560. 			Console.Error.WriteLine( );
  561.  
  562. 			Console.Error.WriteLine( "Notes: [1] This program requires a \"regular\" (MSI based) Microsoft Excel" );
  563.  
  564. 			Console.Error.WriteLine( "           (2007 or later) installation, it will fail on an MS Office" );
  565.  
  566. 			Console.Error.WriteLine( "           \"click-to-run\" installation" );
  567.  
  568. 			Console.Error.WriteLine( "       [2] For Excel 2007, to save as PDF or XPS, this program requires the" );
  569.  
  570. 			Console.Error.WriteLine( "           \"Microsoft Save as PDF or XPS Add-in for 2007 Microsoft Office" );
  571.  
  572. 			Console.Error.WriteLine( "           programs\", available at:" );
  573.  
  574. 			Console.Error.WriteLine( "           http://www.microsoft.com/en-us/download/details.aspx?id=7" );
  575.  
  576. 			Console.Error.WriteLine( "       [3] If wildcards are used in the Excel file names, and the output file" );
  577.  
  578. 			Console.Error.Write( "           path is not specified, " );
  579. 			Console.ForegroundColor = ConsoleColor.White;
  580. 			Console.Error.Write( "/T:type" );
  581. 			Console.ResetColor( );
  582. 			Console.Error.WriteLine( " must be used, and the input file" );
  583.  
  584. 			Console.Error.WriteLine( "           names should not contain dots." );
  585.  
  586. 			Console.Error.WriteLine( "       [4] If wildcards are used in the Excel file names, and the output file" );
  587.  
  588. 			Console.Error.WriteLine( "           path is specified, the output file name must be \"*\"." );
  589.  
  590. 			Console.Error.Write( "       [5] If wildcards are used in the Excel file names, and the " );
  591. 			Console.ForegroundColor = ConsoleColor.White;
  592. 			Console.Error.Write( "/O" );
  593. 			Console.ResetColor( );
  594. 			Console.Error.WriteLine( " switch" );
  595.  
  596. 			Console.Error.WriteLine( "           is not used, the program will display an error message in case an" );
  597.  
  598. 			Console.Error.WriteLine( "           output file already exists, but it will then continue to convert" );
  599.  
  600. 			Console.Error.WriteLine( "           the next file instead of aborting." );
  601.  
  602. 			Console.Error.WriteLine( "       [6] If Excel was already active when this program is started, any" );
  603.  
  604. 			Console.Error.WriteLine( "           other opened spreadsheet(s) will be left alone, and only the" );
  605.  
  606. 			Console.Error.WriteLine( "           spreadsheet(s) opened by this program will be closed." );
  607.  
  608. 			Console.Error.WriteLine( );
  609.  
  610. 			Console.Error.Write( "Examples:  " );
  611. 			Console.ForegroundColor = ConsoleColor.White;
  612. 			Console.Error.WriteLine( "EXCEL2ANY \"D:\\folder\\myfile.xls\" *.pdf" );
  613. 			Console.ResetColor( );
  614.  
  615. 			Console.Error.WriteLine( "           will save to \"D:\\folder\\myfile.pdf\"" );
  616.  
  617. 			Console.Error.WriteLine( );
  618.  
  619. 			Console.ForegroundColor = ConsoleColor.White;
  620. 			Console.Error.WriteLine( "           EXCEL2ANY \"D:\\folder\\myfile.xlsx\" \"D:\\otherfolder\\*.ods\"" );
  621. 			Console.ResetColor( );
  622.  
  623. 			Console.Error.WriteLine( "           will save to \"D:\\otherfolder\\myfile.ods\"" );
  624.  
  625. 			Console.Error.WriteLine( );
  626.  
  627. 			Console.ForegroundColor = ConsoleColor.White;
  628. 			Console.Error.WriteLine( "           EXCEL2ANY \"D:\\folder\\myfile.xlsx\" \"D:\\elsewhere\\newfile.xps\"" );
  629. 			Console.ResetColor( );
  630.  
  631. 			Console.Error.WriteLine( "           will save to \"D:\\elsewhere\\newfile.xps\"" );
  632.  
  633. 			Console.Error.WriteLine( );
  634.  
  635. 			Console.ForegroundColor = ConsoleColor.White;
  636. 			Console.Error.WriteLine( "           EXCEL2ANY \"D:\\folder\\name*.xlsx\" *.html" );
  637. 			Console.ResetColor( );
  638.  
  639. 			Console.Error.WriteLine( "           will save all matching files as HTML to \"D:\\folder\\\"" );
  640.  
  641. 			Console.Error.WriteLine( "           recognized extensions: csv, htm(l), ods, pdf, txt, xls(x), xlsm, xps" );
  642.  
  643. 			Console.Error.WriteLine( );
  644.  
  645. 			Console.ForegroundColor = ConsoleColor.White;
  646. 			Console.Error.WriteLine( "           EXCEL2ANY \"D:\\folder\\*.xlsx\" /T:8" );
  647. 			Console.ResetColor( );
  648.  
  649. 			Console.Error.WriteLine( "           like previous example, but more file types available" );
  650.  
  651. 			Console.Error.WriteLine( );
  652.  
  653. 			Console.ForegroundColor = ConsoleColor.White;
  654. 			Console.Error.WriteLine( "           EXCEL2ANY /T" );
  655. 			Console.ResetColor( );
  656.  
  657. 			Console.Error.WriteLine( "           will list all available file types" );
  658.  
  659. 			Console.Error.WriteLine( );
  660.  
  661. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  662.  
  663. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  664.  
  665. 			return 1;
  666. 		}
  667.  
  668.  
  669. 		// Returns -1 if folder not found, 0 if no matching files found, otherwise the matching file count
  670. 		static int ValidateFilespec( string filespec )
  671. 		{
  672. 			int matchingfiles = -1;
  673. 			try
  674. 			{
  675. 				string parentfolder = Directory.GetParent( filespec ).FullName;
  676. 				if ( Directory.Exists( parentfolder ) )
  677. 				{
  678. 					matchingfiles = 0;
  679. 					foreach ( string matchingfile in Directory.GetFiles( parentfolder, Path.GetFileName( filespec ) ) )
  680. 					{
  681. 						matchingfiles += 1;
  682. 					}
  683. 				}
  684. 			}
  685. 			catch { };
  686. 			return matchingfiles;
  687. 		}
  688. 	}
  689. }
  690.  

page last modified: 2024-04-16; loaded in 0.0224 seconds