月度归档:2014年03月

PHPExcel 单元格格式操作对照表

$B1_Style = $objActSheet->getStyle('B1');

获取单元格格式对象,如下图:
Excel单元格格式
这里面一共有6个Tab,分别对应如下:

getNumberFormat()
getAlignment()
getFont() 
getBorders()
getFill()
getProtection() 

调用举例:

$objActSheet->getStyle('B1')-> getAlignment();

每个Tab对应的辅助类在PHPExcel_Style中对应,比如要应用一个格式:

$objActSheet->getStyle('B1')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER);

以下针对6个Tab可用操作作对比:

//getNumberFormat()			
setFormatCode() 

Excel单元格格式 - 数字格式

//getAlignment()
setHorizontal() 
setVertical() 
setTextRotation() 
setWrapText() 
setShrinkToFit() 
setIndent() 

Excel单元格格式 - 对齐

//getFont()
setName() 
setBold() 
setItalic() 
setUnderline() 
setStrikethrough()
getColor() 
setSize() 
setSuperScript() 
setSubScript() 

Excel单元格格式 - 字体

//getBorders()
getLeft() 
getRight() 
getTop() 
getBottom() 
getDiagonal() 
getVertical() 
getHorizontal() 
setDiagonalDirection() 
setOutline() 

Excel单元格格式 - 边框

//getFill()
setFillType() 
setRotation() 
getStartColor() 
getEndColor() 
getStartColor() 

Excel单元格格式 - 填充

//getProtection() 
setLocked() 
setHidden() 

Excel单元格格式 - 保护

PHPExcel用户文档_部分中文

1 Spreadsheet File Formats 电子表格文件格式

PHPExcel 支持以下的文件格式:

Excel5
The Microsoft Excel™ Binary file format (BIFF5 and BIFF8) is a binary file format that was used by Microsoft Excel™ between versions 95 and 2003. The format is supported (to various extents) by most spreadsheet programs. BIFF files normally have an extension of .xls. Documentation describing the format can be found online at http://msdn.microsoft.com/en-us/library/cc313154(v=office.12).aspx or from http://download.microsoft.com/download/2/4/8/24862317-78F0-4C4B-B355-C7B2C1D997DB/[MS-XLS].pdf (as a downloadable PDF).

Excel2003XML
Microsoft Excel™ 2003 included options for a file format called SpreadsheetML. This file is a zipped XML document. It is not very common, but its core features are supported. Documentation for the format can be found at http://msdn.microsoft.com/en-us/library/aa140066%28office.10%29.aspx though it’s sadly rather sparse in its detail.

Excel2007
Microsoft Excel™ 2007 shipped with a new file format, namely Microsoft Office Open XML SpreadsheetML, and Excel 2010 extended this still further with its new features such as sparklines. These files typically have an extension of .xlsx. This format is based around a zipped collection of eXtensible Markup Language (XML) files. Microsoft Office Open XML SpreadsheetML is mostly standardized in ECMA 376
(http://www.ecma-international.org/news/TC45_current_work/TC45_available_docs.htm) and ISO 29500.

……

2 Security
3 Loading a Spreadsheet File加载电子表格文件
The simplest way to load a workbook file is to let PHPExcel’s IO Factory identify the file type and load it, calling the static load() method of the PHPExcel_IOFactory class.(最简单的加载一个工作薄文件的方法是让 PHPExcel’s IO Factory验证文件类型和加载它,通过调用PHPExcel_IOFactory类的静态方法load来实现这个过程)

$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);

The load() method will attempt to identify the file type, and instantiate a loader for that file type方法load()将试图验证文件的类型,并且会针对文件类型实例化一个装载器; using it to load the file and store the data and any formatting in a PHPExcel object 使用它来装载文件和存储任意格式的数据到一个PHPExcel对象中.
The method makes an initial guess at the loader to instantiate based on the file extension 这个方法根据文件的扩展名来初步猜测要实例化的加载器; but will test the file before actually executing the load但是在实际执行加载之前会测试文件: so if (for example) the file is actually a CSV file that has been given a .xls extension (quite a common practise)所以如果文件实际是CSV文件但是用了xls作为扩展名, it will reject the Excel5 loader that it would normally use for a .xls file那么它会拒绝使用在正常情况下为xls文件使用的Excel5加载器; and test the file using the other loaders until it finds the appropriate loader然后使用其它的装载器测试文件直到找到合适的装载器, and then use that to read the file然后使用找到的装载器读取文件.
While easy to implement in your code, and you don’t need to worry about the file type这样当在你的快速实现的代码中,你不需要担心文件的类型; this isn’t the most efficient method to load a file但是这不是一个最高效率的装载文件的方法; and it lacks the flexibility to configure the loader in any way before actually reading the file into a PHPExcel object并且在实际读取文件到一个PHPExcel对象中之前,缺乏了可以在任何情况下去配置一个装载器的灵活性。

4 Creating a Reader and Loading a Spreadsheet File 创建一个阅读器并且装载一个电子表格

If you know the file type of the spreadsheet file that you need to load如果你知道你需要装载的电子表格的文件的文件类型, you can instantiate a new reader object for that file type你可以为这种文件类型实例化一个新的阅读器对象, then use the reader’s load() method to read the file to a PHPExcel object然后使用阅读器的load()方法来读取文件到一个PHPExcel对象中. It is possible to instantiate the reader objects for each of the different supported filetype by name可实例化的阅读器对象为每个不同的被支持的文件类型的名字. However, you may get unpredictable results if the file isn’t of the right type 但是,如果文件不是给定正确的类型,你可能会获取一个不可预知的结果(e.g. it is a CSV with an extension of .xls), although this type of exception should normally be trapped 尽管这种类型的异常能正确被扑捉到.

/** Create a new Excel5 Reader **/ 
$objReader = new PHPExcel_Reader_Excel5();
// $objReader = new PHPExcel_Reader_Excel2007(); 
// $objReader = new PHPExcel_Reader_Excel2003XML(); 
// $objReader = new PHPExcel_Reader_OOCalc(); 
// $objReader = new PHPExcel_Reader_SYLK(); 
// $objReader = new PHPExcel_Reader_Gnumeric(); 
// $objReader = new PHPExcel_Reader_CSV(); 

/** Load $inputFileName to a PHPExcel Object **/ 
$objPHPExcel = $objReader->load($inputFileName);

Alternatively, you can use the IO Factory’s createReader() method to instantiate the reader object for you, simply telling it the file type of the reader that you want instantiating也可以使用IO工厂的createRead()方法实例一个阅读器对象:

$inputFileType = 'Excel5'; 
// $inputFileType = 'Excel2007'; 
// $inputFileType = 'Excel2003XML'; 
// $inputFileType = 'OOCalc'; 
// $inputFileType = 'SYLK'; 
// $inputFileType = 'Gnumeric'; 
// $inputFileType = 'CSV'; 

$inputFileName = './sampleData/example1.xls'; 

/** Create a new Reader of the type defined in $inputFileType **/ 
$objReader = PHPExcel_IOFactory::createReader($inputFileType); 

/** Load $inputFileName to a PHPExcel Object **/ 
$objPHPExcel = $objReader->load($inputFileName);

.

If you’re uncertain of the filetype, you can use the IO Factory’s identify() method to identify the reader that you need, before using the createReader() method to instantiate the reader object. 如果你不确定文件类型,在使用createReader()方法实例化阅读器对象之前,你可以使用IO工厂的identify()方法去确定你需要的阅读器(这个是最好的方法):

$inputFileName = './sampleData/example1.xls'; 

/** Identify the type of $inputFileName **/ 
$inputFileType = PHPExcel_IOFactory::identify($inputFileName); 

/** Create a new Reader of the type that has been identified **/ 
$objReader = PHPExcel_IOFactory::createReader($inputFileType); 

/** Load $inputFileName to a PHPExcel Object **/ 
$objPHPExcel = $objReader->load($inputFileName);

5 Spreadsheet Reader Options 电子表格阅读器选项
Once you have created a reader object for the workbook that you want to load, you have the opportunity to set additional options before executing the load() method.一旦你已经为一个你想加载的工作薄创建了一个阅读器对象,在执行load()方法之前,你有机会去设置附加的选项。

5.1 Reading Only Data from a Spreadsheet File 从电子表格文件中只读取数据

If you’re only interested in the cell values in a workbook, but don’t need any of the cell formatting information, then you can set the reader to read only the data values and any formulae from each cell using the setReadDataOnly() method. 如果你只对工作薄中的单元格的值感兴趣,对单元格的任何格式信息都不需要,你可以通过使用setReadDataOnly()方法来设置阅读器从单元格中仅仅读取数据值和所有公式表单。

$inputFileType = 'Excel5'; 
$inputFileName = './sampleData/example1.xls'; 

/** Create a new Reader of the type defined in $inputFileType **/ 
$objReader = PHPExcel_IOFactory::createReader($inputFileType);

/** Advise the Reader that we only want to load cell data **/ 
$objReader->setReadDataOnly(true); 

/** Load $inputFileName to a PHPExcel Object **/ 
$objPHPExcel = $objReader->load($inputFileName);

It is important to note that Workbooks (and PHPExcel) store dates and times as simple numeric values: they can only be distinguished from other numeric values by the format mask that is applied to that cell. 工作薄(和PHPExcel)存储日期和时间作为简单数字值:它们和其它数字值的区别是当它应用了格式掩码时 When setting read data only to true, PHPExcel doesn’t read the cell format masks, so it is not possible to differentiate between dates/times and numbers. 当设置为只读数据为真时,PHPExcel不会读取单元格的格式掩码,所以它不可能区分日期/时间和数字之间的不同。
The Gnumeric loader has been written to read the format masks for date values even when read data only has been set to true, so it can differentiate between dates/times and numbers; but this change hasn’t yet been implemented for the other readers. Gnumeric加载器在读取日期值是被重写要求去读取格式掩码,就算是它被为只读数据位真是时,所以就可以区分日期/时间和数字的不同; 但是这个改变对其它阅读器还没有实现(就是其它的阅读器如果设置了只读数据,那么无法区分日期时间和数字的区别)。


5.2 Reading Only Named WorkSheets from a File 从文件中只读被命名的工作表

If your workbook contains a number of worksheets, but you are only interested in reading some of those, then you can use the setLoadSheetsOnly() method to identify those sheets you are interested in reading. 如果你的工作薄包含一系列的工作表,但你只有兴趣去读取其中的一些,这样你就可以使用setLoadSheetsOnly()方法去验证那些你感兴趣读取的表格。

To read a single sheet, you can pass that sheet name as a parameter to the setLoadSheetsOnly() method. 如果只读取单个表格,你可以把表格的名字作为参数传递到setLoadSheetsOnly()方法。

$inputFileType = 'Excel5'; 
$inputFileName = './sampleData/example1.xls'; 
$sheetname = 'Data Sheet #2'; 
// $sheetnames = array('Data Sheet #1','Data Sheet #3');

/** Create a new Reader of the type defined in $inputFileType **/ 
$objReader = PHPExcel_IOFactory::createReader($inputFileType); 

/** Advise the Reader of which WorkSheets we want to load **/ 
$objReader->setLoadSheetsOnly($sheetname); 

/** Load $inputFileName to a PHPExcel Object **/ 
$objPHPExcel = $objReader->load($inputFileName);

To reset this option to the default, you can call the setLoadAllSheets() method. 为了重置这个选项到默认,你可以调用setLoadAllSheets()方法。

5.3 Reading Only Specific Columns and Rows from a File (Read Filters) 从文件中只读取指定的列和行

If you are only interested in reading part of a worksheet, then you can write a filter class that identifies whether or not individual cells should be read by the loader. 如果你只有兴趣读取工作表的一部分,你可以写一个过滤类用来是否验证单独的单元格通过加载器读取 A read filter must implement the PHPExcel_Reader_IReadFilter interface, and contain a readCell() method that accepts arguments of $column, $row and $worksheetName, and return a boolean true or false that indicates whether a workbook cell identified by those arguments should be read or not. 一个阅读器过滤器必须实现PHPExcel_Reader_IReadFilter接口,并且包含readCell()方法并且接受$column, $row and $worksheetName参数,返回true or false。

$inputFileType = 'Excel5'; 
$inputFileName = './sampleData/example1.xls'; 
$sheetname = 'Data Sheet #3'; 

/** Define a Read Filter class implementing PHPExcel_Reader_IReadFilter */ 
class MyReadFilter implements PHPExcel_Reader_IReadFilter {
public function readCell($column, $row, $worksheetName = '') { 
// Read rows 1 to 7 and columns A to E only 
if ($row >= 1 && $row <= 7) {
if (in_array($column,range('A','E'))) { return true; } 
} 
return false; 
} 
}

/** Create an Instance of our Read Filter **/ 
$filterSubset = new MyReadFilter(); 

/** Create a new Reader of the type defined in $inputFileType **/ 
$objReader = PHPExcel_IOFactory::createReader($inputFileType); 

/** Tell the Reader that we want to use the Read Filter **/ 
$objReader->setReadFilter($filterSubset); 

/** Load only the rows and columns that match our filter to PHPExcel **/ 
$objPHPExcel = $objReader->load($inputFileName);

This example is not particularly useful, because it can only be used in a very specific circumstance (when you only want cells in the range A1:E7 from your worksheet. A generic Read Filter would probably be more useful(更加有用的方法如下)

/** Define a Read Filter class implementing PHPExcel_Reader_IReadFilter */ 
class MyReadFilter implements PHPExcel_Reader_IReadFilter { 
private $_startRow = 0; 
private $_endRow = 0; 
private $_columns = array(); 

/** Get the list of rows and columns to read */ 
public function __construct($startRow, $endRow, $columns) { 
$this->_startRow = $startRow; 
$this->_endRow = $endRow; 
$this->_columns = $columns; 
}
public function readCell($column, $row, $worksheetName = '') { 
// Only read the rows and columns that were configured 
if ($row >= $this->_startRow && $row <= $this->_endRow) { 
if (in_array($column,$this->_columns)) { return true; } }
return false; 
}
} 
/** Create an Instance of our Read Filter, passing in the cell range **/ 
$filterSubset = new MyReadFilter(9,15,range('G','K'));
}

5.4 Combining Multiple Files into a Single PHPExcel Object
5.5. Combining Read Filters with the setSheetIndex() method to split a large CSV file across multiple Worksheets
5.6. Pipe or Tab Separated Value Files
5.7. A Brief Word about the Advanced Value Binder
When loading data from a file that contains no formatting information, such as a CSV file, then data is read either as strings or numbers (float or integer). This means that PHPExcel does not automatically recognise dates/times (such as “16-Apr-2009” or “13:30”), booleans (“TRUE” or “FALSE”), percentages (“75%”), hyperlinks (“http://www.phpexcel.net”), etc as anything other than simple strings. However, you can apply additional processing that is executed against these values during the load process within a Value Binder.
A Value Binder is a class that implement the PHPExcel_Cell_IValueBinder interface. It must contain a bindValue() method that accepts a PHPExcel_Cell and a value as arguments, and return a boolean true or false that indicates whether the workbook cell has been populated with the value or not. The Advanced Value Binder implements such a class: amongst other tests, it identifies a string comprising “TRUE” or “FALSE” (based on locale settings) and sets it to a boolean; or a number in scientific format (e.g. “1.234e-5”) and converts it to a float; or dates and times, converting them to their Excel timestamp value – before storing the value in the cell object. It also sets formatting for strings that are identified as dates, times or percentages. It could easily be extended to provide additional handling (including text or cell formatting) when it encountered a hyperlink, or HTML markup within a CSV file.
So using a Value Binder allows a great deal more flexibility in the loader logic when reading unformatted text files.

6. Error Handling
Of course, you should always apply some error handling to your scripts as well. PHPExcel throws exceptions, so you can wrap all your code that accesses the library methods within Try/Catch blocks to trap for any problems that are encountered, and deal with them in an appropriate manner.
The PHPExcel Readers throw a PHPExcel_Reader_Exception.

$inputFileName = './sampleData/example-1.xls'; 
try { 
/** Load $inputFileName to a PHPExcel Object **/ 
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName); 
} catch(PHPExcel_Reader_Exception $e) { 
die('Error loading file: '.$e->getMessage()); 
}

注意抛出异常的类型。

7. Helper Methods
You can retrieve a list of worksheet names contained in a file without loading the whole file by using the Reader’s listWorksheetNames() method; similarly, a listWorksheetInfo() method will retrieve the dimensions尺寸 of worksheet in a file without needing to load and parse the whole file.

The listWorksheetNames() method returns a simple array listing each worksheet name within the workbook:

The listWorksheetInfo() method returns a nested array, with each entry listing the name and dimensions for a worksheet:

$objReader = PHPExcel_IOFactory::createReader($inputFileType); 
$worksheetData = $objReader->listWorksheetInfo($inputFileName); 

echo '<h3>Worksheet Information</h3>'; 
echo '<ol>'; 
foreach ($worksheetData as $worksheet) { 
echo '<li>', $worksheet['worksheetName'], '<br />'; 
echo 'Rows: ', $worksheet['totalRows'], 
' Columns: ', $worksheet['totalColumns'], '<br />'; 
echo 'Cell Range: A1:', 
$worksheet['lastColumnLetter'], $worksheet['totalRows']; 
echo '</li>'; 
} 
echo '</ol>';

这个worksheetName和totalRows和totalColumns是非常有用的。

Linux 作业管理(job control)

当登录系统取得bash shell之后,在单一终端机下同时进行多个作业的行为管理。

在进行作业管理的行为中,其实每个作业都是目前bash的子进程,即彼此之间是有相关性的。登录一个终端出现提示符让你操作的环境成为前台 (foreground),至于其它作业就可以放入后台(background)去暂停或运行。注意:放入后台的作业必须不能与用户互动。

Bash的作业控制必须要注意到的限制是:
这些作业所触发的进程必须来自于你shell的子进程
前台:你可以控制于执行命令的这个环境成为前台的作业
后台:可以自行运行的作业,你无法使用[ctrl+c]终止它,可使用bg/fg调用该作业
后台中执行的进程不能等待terminal/shell的输入

1 Job control管理 [作业管理]
Bash下面的Job分前台与后台,然后再后台里面的作业状态又可分为暂停与运行中。

直接将命令丢到后台中执行的&
例子:

tar –zpcf /tmp/etc/targ.gz /etc &

后面加入上&表示在后台处理,回车后看到bash会给予这个命令一个作业号码(job number),就是那个[1]。但是这个标准输出和错误输出还是都屏幕上。所以应该使用重定向处理。

tar –zpcf /tmp/etc.tar.gz /etc/ > /tmp/log.txt 2>&1 &

将目前的作业丢到后台中暂停:ctrl-z
比如正在使用vi,却发现有个文件不知道放在哪里,需要到bash环境下进行查找,此时只要将vi丢到后台当中等待即可。
例子:

vi ~/.bashrc
# 在vi的一般模式下,按下[ctrl]-z这两个键
[1]+ Stopped
# 顺利取得前台控制权

按下[ctrl]-z这两个键,屏幕上出现[1],表示这是第一个作业,而那个+代表最近一个被丢进后台的作业,且目前在后台下默认会被取用的那个作业(与fg这个命令有关)。而那个stopped则代表目前这个作业的状态。默认,使用[ctrl-z]丢到后台当中的作业都是暂停状态。

查看目前的后台作业状态:jobs
用法:

jobs [-lrs]
参数:
-l 除了列出job number与命令串之外,同时列出PID的号码
-r 仅列出正在后台run的作业
-s 仅列出正在后台当中暂停(stop)的作业

一般,直接执行jobs即可。如果还想知道该Job number的PID号码,可以加上-l这个参数。在输出的信息中,那个+代表默认的取用作业。如果目前有两个作业在后台当中,两个作业都是暂停的,而如果仅输入fg时,那么那个标志了+的作业会被拿到前台当中来处理。其实+代表最近被放到后台的作业号码,-代表最近第二个被放置到后台当中的作业号码。而超过最后第三个以后的作业,就不会有+/-符号存在了。

将后台作业拿到前台来处理:fg

fg %jobnumber
参数:
%jobnumber: jobnumber为作业号码(数字)。注意:%可有可无
如果fg –则把标志了-的那个作业拿出来。


让作业在后台下的状态变成运行中:bg

由于按下ctrl-z后作业会放到后台停止,如果要让其继续在后台运行起来,需要使用bg,它和fg的用法一样。

管理后台当中的作业:kill
如果需要删除某个作业,需要使用kill。

用法:
kill –signal %jobnumber
参数:
-l 列出目前能够使用的信号(signal)有哪些
signal 代表给予后面接的那个作业什么样的指示。
-1 重新读取一次参数的配置文件
-2 代表与由键盘输入ctrl-c同样的操作
-9 立刻强制删除一个作业
-15 以正常的程序方式终止一项作业

注意:-9通常是在强制删除一个不正常的作业时所使用的,-15则是以正常步骤接受一项作业(15也是默认值)
另外,kill后面接的数字默认是PID,如果想要管理bash的作业控制,就得要加上%数字了。

永久链接:http://blog.ifeeline.com/1052.html

Linux 终端登录

[root@vfeelit ~]# pstree -an
init
  ├─sshd
  │   ├─sshd 
  │   │   └─bash
  │   └─sshd 
  │       └─bash
  │           └─pstree -an
  ├─login     
  │   └─bash
  ├─mingetty /dev/tty2
  ├─mingetty /dev/tty3
  ├─mingetty /dev/tty4
  ├─mingetty /dev/tty5
  └─mingetty /dev/tty6

默认启动了6个终端(tty)等待用户登录。6个终端命名分别是tty1-tty6,系统开机后默认进入tty1等待用户登录。可以通过使用ALT+F1-F6切换到不同的终端。登录后如果使用pstree查看可知对应的终端“名称”为login并在其下产生了一个bash子进程。实际这个login名称可以看做是对应tty的别名。

和由终端进入bash环境不同,如果通过sshd进入bash的话,首先由主sshd派生一个子进程,然后在这个子进程下再产生bash子进程。实际上,有主sshd派生的子sshd进程,它们是用pts/0-pts/n来做别名的。

永久链接: http://blog.ifeeline.com/1050.html