Examples.pm 395 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666
  1. # DISCLAIMER OF WARRANTY
  2. # Because this software is licensed free of charge, there is no warranty for the software,
  3. # to the extent permitted by applicable law. Except when otherwise stated in writing
  4. # the copyright holders and/or other parties provide the software "as is" without
  5. # warranty of any kind, either expressed or implied, including, but not limited to,
  6. # the implied warranties of merchantability and fitness for a particular purpose.
  7. # The entire risk as to the quality and performance of the software is with you.
  8. # Should the software prove defective, you assume the cost of all necessary
  9. # servicing, repair, or correction.
  10. # In no event unless required by applicable law or agreed to in writing will any
  11. # copyright holder, or any other party who may modify and/or redistribute the software
  12. # as permitted by the above licence, be liable to you for damages, including any general,
  13. # special, incidental, or consequential damages arising out of the use or inability
  14. # to use the software (including but not limited to loss of data or data being rendered
  15. # inaccurate or losses sustained by you or third parties or a failure of the software
  16. # to operate with any other software), even if such holder or other party
  17. # has been advised of the possibility of such damages.
  18. # AUTHOR
  19. # John McNamara jmcnamara@cpan.org
  20. # COPYRIGHT
  21. # Copyright MM-MMX, John McNamara.
  22. # All Rights Reserved. This module is free software. It may be used,
  23. # redistributed and/or modified under the terms of
  24. # the Artistic License(full text of the Artistic License http://dev.perl.org/licenses/artistic.html).
  25. package Spreadsheet::WriteExcel::Examples;
  26. ###############################################################################
  27. #
  28. # Examples - Spreadsheet::WriteExcel examples.
  29. #
  30. # A documentation only module showing the examples that are
  31. # included in the Spreadsheet::WriteExcel distribution. This
  32. # file was generated automatically via the gen_examples_pod.pl
  33. # program that is also included in the examples directory.
  34. #
  35. # Copyright 2000-2010, John McNamara, jmcnamara@cpan.org
  36. #
  37. # Documentation after __END__
  38. #
  39. use strict;
  40. use vars qw($VERSION);
  41. $VERSION = '2.37';
  42. 1;
  43. __END__
  44. =pod
  45. =head1 NAME
  46. Examples - Spreadsheet::WriteExcel example programs.
  47. =head1 DESCRIPTION
  48. This is a documentation only module showing the examples that are
  49. included in the L<Spreadsheet::WriteExcel> distribution.
  50. This file was auto-generated via the gen_examples_pod.pl
  51. program that is also included in the examples directory.
  52. =head1 Example programs
  53. The following is a list of the 85 example programs that are included in the Spreadsheet::WriteExcel distribution.
  54. =over
  55. =item * L<Example: a_simple.pl> A get started example with some basic features.
  56. =item * L<Example: demo.pl> A demo of some of the available features.
  57. =item * L<Example: regions.pl> A simple example of multiple worksheets.
  58. =item * L<Example: stats.pl> Basic formulas and functions.
  59. =item * L<Example: formats.pl> All the available formatting on several worksheets.
  60. =item * L<Example: bug_report.pl> A template for submitting bug reports.
  61. =item * L<Example: autofilter.pl> Examples of worksheet autofilters.
  62. =item * L<Example: autofit.pl> Simulate Excel's autofit for column widths.
  63. =item * L<Example: bigfile.pl> Write past the 7MB limit with OLE::Storage_Lite.
  64. =item * L<Example: cgi.pl> A simple CGI program.
  65. =item * L<Example: chart_area.pl> A demo of area style charts.
  66. =item * L<Example: chart_bar.pl> A demo of bar (vertical histogram) style charts.
  67. =item * L<Example: chart_column.pl> A demo of column (histogram) style charts.
  68. =item * L<Example: chart_line.pl> A demo of line style charts.
  69. =item * L<Example: chart_pie.pl> A demo of pie style charts.
  70. =item * L<Example: chart_scatter.pl> A demo of scatter style charts.
  71. =item * L<Example: chart_stock.pl> A demo of stock style charts.
  72. =item * L<Example: chess.pl> An example of reusing formatting via properties.
  73. =item * L<Example: colors.pl> A demo of the colour palette and named colours.
  74. =item * L<Example: comments1.pl> Add comments to worksheet cells.
  75. =item * L<Example: comments2.pl> Add comments with advanced options.
  76. =item * L<Example: copyformat.pl> Example of copying a cell format.
  77. =item * L<Example: data_validate.pl> An example of data validation and dropdown lists.
  78. =item * L<Example: date_time.pl> Write dates and times with write_date_time().
  79. =item * L<Example: defined_name.pl> Example of how to create defined names.
  80. =item * L<Example: diag_border.pl> A simple example of diagonal cell borders.
  81. =item * L<Example: easter_egg.pl> Expose the Excel97 flight simulator.
  82. =item * L<Example: filehandle.pl> Examples of working with filehandles.
  83. =item * L<Example: formula_result.pl> Formulas with user specified results.
  84. =item * L<Example: headers.pl> Examples of worksheet headers and footers.
  85. =item * L<Example: hide_sheet.pl> Simple example of hiding a worksheet.
  86. =item * L<Example: hyperlink1.pl> Shows how to create web hyperlinks.
  87. =item * L<Example: hyperlink2.pl> Examples of internal and external hyperlinks.
  88. =item * L<Example: images.pl> Adding images to worksheets.
  89. =item * L<Example: indent.pl> An example of cell indentation.
  90. =item * L<Example: merge1.pl> A simple example of cell merging.
  91. =item * L<Example: merge2.pl> A simple example of cell merging with formatting.
  92. =item * L<Example: merge3.pl> Add hyperlinks to merged cells.
  93. =item * L<Example: merge4.pl> An advanced example of merging with formatting.
  94. =item * L<Example: merge5.pl> An advanced example of merging with formatting.
  95. =item * L<Example: merge6.pl> An example of merging with Unicode strings.
  96. =item * L<Example: mod_perl1.pl> A simple mod_perl 1 program.
  97. =item * L<Example: mod_perl2.pl> A simple mod_perl 2 program.
  98. =item * L<Example: outline.pl> An example of outlines and grouping.
  99. =item * L<Example: outline_collapsed.pl> An example of collapsed outlines.
  100. =item * L<Example: panes.pl> An examples of how to create panes.
  101. =item * L<Example: properties.pl> Add document properties to a workbook.
  102. =item * L<Example: protection.pl> Example of cell locking and formula hiding.
  103. =item * L<Example: repeat.pl> Example of writing repeated formulas.
  104. =item * L<Example: right_to_left.pl> Change default sheet direction to right to left.
  105. =item * L<Example: row_wrap.pl> How to wrap data from one worksheet onto another.
  106. =item * L<Example: sales.pl> An example of a simple sales spreadsheet.
  107. =item * L<Example: sendmail.pl> Send an Excel email attachment using Mail::Sender.
  108. =item * L<Example: stats_ext.pl> Same as stats.pl with external references.
  109. =item * L<Example: stocks.pl> Demonstrates conditional formatting.
  110. =item * L<Example: tab_colors.pl> Example of how to set worksheet tab colours.
  111. =item * L<Example: textwrap.pl> Demonstrates text wrapping options.
  112. =item * L<Example: win32ole.pl> A sample Win32::OLE example for comparison.
  113. =item * L<Example: write_arrays.pl> Example of writing 1D or 2D arrays of data.
  114. =item * L<Example: write_handler1.pl> Example of extending the write() method. Step 1.
  115. =item * L<Example: write_handler2.pl> Example of extending the write() method. Step 2.
  116. =item * L<Example: write_handler3.pl> Example of extending the write() method. Step 3.
  117. =item * L<Example: write_handler4.pl> Example of extending the write() method. Step 4.
  118. =item * L<Example: write_to_scalar.pl> Example of writing an Excel file to a Perl scalar.
  119. =item * L<Example: unicode_utf16.pl> Simple example of using Unicode UTF16 strings.
  120. =item * L<Example: unicode_utf16_japan.pl> Write Japanese Unicode strings using UTF-16.
  121. =item * L<Example: unicode_cyrillic.pl> Write Russian Cyrillic strings using UTF-8.
  122. =item * L<Example: unicode_list.pl> List the chars in a Unicode font.
  123. =item * L<Example: unicode_2022_jp.pl> Japanese: ISO-2022-JP to utf8 in perl 5.8.
  124. =item * L<Example: unicode_8859_11.pl> Thai: ISO-8859_11 to utf8 in perl 5.8.
  125. =item * L<Example: unicode_8859_7.pl> Greek: ISO-8859_7 to utf8 in perl 5.8.
  126. =item * L<Example: unicode_big5.pl> Chinese: BIG5 to utf8 in perl 5.8.
  127. =item * L<Example: unicode_cp1251.pl> Russian: CP1251 to utf8 in perl 5.8.
  128. =item * L<Example: unicode_cp1256.pl> Arabic: CP1256 to utf8 in perl 5.8.
  129. =item * L<Example: unicode_koi8r.pl> Russian: KOI8-R to utf8 in perl 5.8.
  130. =item * L<Example: unicode_polish_utf8.pl> Polish : UTF8 to utf8 in perl 5.8.
  131. =item * L<Example: unicode_shift_jis.pl> Japanese: Shift JIS to utf8 in perl 5.8.
  132. =item * L<Example: csv2xls.pl> Program to convert a CSV file to an Excel file.
  133. =item * L<Example: tab2xls.pl> Program to convert a tab separated file to xls.
  134. =item * L<Example: datecalc1.pl> Convert Unix/Perl time to Excel time.
  135. =item * L<Example: datecalc2.pl> Calculate an Excel date using Date::Calc.
  136. =item * L<Example: lecxe.pl> Convert Excel to WriteExcel using Win32::OLE.
  137. =item * L<Example: convertA1.pl> Helper functions for dealing with A1 notation.
  138. =item * L<Example: function_locale.pl> Add non-English function names to Formula.pm.
  139. =item * L<Example: writeA1.pl> Example of how to extend the module.
  140. =back
  141. =head2 Example: a_simple.pl
  142. A simple example of how to use the Spreadsheet::WriteExcel module to write
  143. some text and numbers to an Excel binary file.
  144. =begin html
  145. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/a_simple.jpg" width="640" height="420" alt="Output from a_simple.pl" /></center></p>
  146. =end html
  147. Source code for this example:
  148. #!/usr/bin/perl -w
  149. ###############################################################################
  150. #
  151. # A simple example of how to use the Spreadsheet::WriteExcel module to write
  152. # some text and numbers to an Excel binary file.
  153. #
  154. # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org
  155. #
  156. use strict;
  157. use Spreadsheet::WriteExcel;
  158. # Create a new workbook called simple.xls and add a worksheet
  159. my $workbook = Spreadsheet::WriteExcel->new('a_simple.xls');
  160. my $worksheet = $workbook->add_worksheet();
  161. # The general syntax is write($row, $column, $token). Note that row and
  162. # column are zero indexed
  163. #
  164. # Write some text
  165. $worksheet->write(0, 0, "Hi Excel!");
  166. # Write some numbers
  167. $worksheet->write(2, 0, 3); # Writes 3
  168. $worksheet->write(3, 0, 3.00000); # Writes 3
  169. $worksheet->write(4, 0, 3.00001); # Writes 3.00001
  170. $worksheet->write(5, 0, 3.14159); # TeX revision no.?
  171. # Write some formulas
  172. $worksheet->write(7, 0, '=A3 + A6');
  173. $worksheet->write(8, 0, '=IF(A5>3,"Yes", "No")');
  174. # Write a hyperlink
  175. $worksheet->write(10, 0, 'http://www.perl.com/');
  176. __END__
  177. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/a_simple.pl>
  178. =head2 Example: demo.pl
  179. A simple demo of some of the features of Spreadsheet::WriteExcel.
  180. This program is used to create the project screenshot for Freshmeat:
  181. L<http://freshmeat.net/projects/writeexcel/>
  182. =begin html
  183. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/demo.jpg" width="640" height="420" alt="Output from demo.pl" /></center></p>
  184. =end html
  185. Source code for this example:
  186. #!/usr/bin/perl -w
  187. #######################################################################
  188. #
  189. # A simple demo of some of the features of Spreadsheet::WriteExcel.
  190. #
  191. # This program is used to create the project screenshot for Freshmeat:
  192. # L<http://freshmeat.net/projects/writeexcel/>
  193. #
  194. # reverse('©'), October 2001, John McNamara, jmcnamara@cpan.org
  195. #
  196. use strict;
  197. use Spreadsheet::WriteExcel;
  198. my $workbook = Spreadsheet::WriteExcel->new("demo.xls");
  199. my $worksheet = $workbook->add_worksheet('Demo');
  200. my $worksheet2 = $workbook->add_worksheet('Another sheet');
  201. my $worksheet3 = $workbook->add_worksheet('And another');
  202. my $bold = $workbook->add_format(bold => 1);
  203. #######################################################################
  204. #
  205. # Write a general heading
  206. #
  207. $worksheet->set_column('A:A', 36, $bold);
  208. $worksheet->set_column('B:B', 20 );
  209. $worksheet->set_row (0, 40 );
  210. my $heading = $workbook->add_format(
  211. bold => 1,
  212. color => 'blue',
  213. size => 16,
  214. merge => 1,
  215. align => 'vcenter',
  216. );
  217. my @headings = ('Features of Spreadsheet::WriteExcel', '');
  218. $worksheet->write_row('A1', \@headings, $heading);
  219. #######################################################################
  220. #
  221. # Some text examples
  222. #
  223. my $text_format = $workbook->add_format(
  224. bold => 1,
  225. italic => 1,
  226. color => 'red',
  227. size => 18,
  228. font =>'Lucida Calligraphy'
  229. );
  230. # A phrase in Cyrillic
  231. my $unicode = pack "H*", "042d0442043e002004440440043004370430002004".
  232. "3d043000200440044304410441043a043e043c0021";
  233. $worksheet->write('A2', "Text");
  234. $worksheet->write('B2', "Hello Excel");
  235. $worksheet->write('A3', "Formatted text");
  236. $worksheet->write('B3', "Hello Excel", $text_format);
  237. $worksheet->write('A4', "Unicode text");
  238. $worksheet->write_utf16be_string('B4', $unicode);
  239. #######################################################################
  240. #
  241. # Some numeric examples
  242. #
  243. my $num1_format = $workbook->add_format(num_format => '$#,##0.00');
  244. my $num2_format = $workbook->add_format(num_format => ' d mmmm yyy');
  245. $worksheet->write('A5', "Numbers");
  246. $worksheet->write('B5', 1234.56);
  247. $worksheet->write('A6', "Formatted numbers");
  248. $worksheet->write('B6', 1234.56, $num1_format);
  249. $worksheet->write('A7', "Formatted numbers");
  250. $worksheet->write('B7', 37257, $num2_format);
  251. #######################################################################
  252. #
  253. # Formulae
  254. #
  255. $worksheet->set_selection('B8');
  256. $worksheet->write('A8', 'Formulas and functions, "=SIN(PI()/4)"');
  257. $worksheet->write('B8', '=SIN(PI()/4)');
  258. #######################################################################
  259. #
  260. # Hyperlinks
  261. #
  262. $worksheet->write('A9', "Hyperlinks");
  263. $worksheet->write('B9', 'http://www.perl.com/' );
  264. #######################################################################
  265. #
  266. # Images
  267. #
  268. $worksheet->write('A10', "Images");
  269. $worksheet->insert_image('B10', 'republic.png', 16, 8);
  270. #######################################################################
  271. #
  272. # Misc
  273. #
  274. $worksheet->write('A18', "Page/printer setup");
  275. $worksheet->write('A19', "Multiple worksheets");
  276. __END__
  277. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/demo.pl>
  278. =head2 Example: regions.pl
  279. An example of how to use the Spreadsheet:WriteExcel module to write a basic
  280. Excel workbook with multiple worksheets.
  281. =begin html
  282. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/regions.jpg" width="640" height="420" alt="Output from regions.pl" /></center></p>
  283. =end html
  284. Source code for this example:
  285. #!/usr/bin/perl -w
  286. ###############################################################################
  287. #
  288. # An example of how to use the Spreadsheet:WriteExcel module to write a basic
  289. # Excel workbook with multiple worksheets.
  290. #
  291. # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org
  292. #
  293. use strict;
  294. use Spreadsheet::WriteExcel;
  295. # Create a new Excel workbook
  296. my $workbook = Spreadsheet::WriteExcel->new("regions.xls");
  297. # Add some worksheets
  298. my $north = $workbook->add_worksheet("North");
  299. my $south = $workbook->add_worksheet("South");
  300. my $east = $workbook->add_worksheet("East");
  301. my $west = $workbook->add_worksheet("West");
  302. # Add a Format
  303. my $format = $workbook->add_format();
  304. $format->set_bold();
  305. $format->set_color('blue');
  306. # Add a caption to each worksheet
  307. foreach my $worksheet ($workbook->sheets()) {
  308. $worksheet->write(0, 0, "Sales", $format);
  309. }
  310. # Write some data
  311. $north->write(0, 1, 200000);
  312. $south->write(0, 1, 100000);
  313. $east->write (0, 1, 150000);
  314. $west->write (0, 1, 100000);
  315. # Set the active worksheet
  316. $south->activate();
  317. # Set the width of the first column
  318. $south->set_column(0, 0, 20);
  319. # Set the active cell
  320. $south->set_selection(0, 1);
  321. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/regions.pl>
  322. =head2 Example: stats.pl
  323. A simple example of how to use functions with the Spreadsheet::WriteExcel
  324. module.
  325. =begin html
  326. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/stats.jpg" width="640" height="420" alt="Output from stats.pl" /></center></p>
  327. =end html
  328. Source code for this example:
  329. #!/usr/bin/perl -w
  330. ###############################################################################
  331. #
  332. # A simple example of how to use functions with the Spreadsheet::WriteExcel
  333. # module.
  334. #
  335. # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org
  336. #
  337. use strict;
  338. use Spreadsheet::WriteExcel;
  339. # Create a new workbook and add a worksheet
  340. my $workbook = Spreadsheet::WriteExcel->new("stats.xls");
  341. my $worksheet = $workbook->add_worksheet('Test data');
  342. # Set the column width for columns 1
  343. $worksheet->set_column(0, 0, 20);
  344. # Create a format for the headings
  345. my $format = $workbook->add_format();
  346. $format->set_bold();
  347. # Write the sample data
  348. $worksheet->write(0, 0, 'Sample', $format);
  349. $worksheet->write(0, 1, 1);
  350. $worksheet->write(0, 2, 2);
  351. $worksheet->write(0, 3, 3);
  352. $worksheet->write(0, 4, 4);
  353. $worksheet->write(0, 5, 5);
  354. $worksheet->write(0, 6, 6);
  355. $worksheet->write(0, 7, 7);
  356. $worksheet->write(0, 8, 8);
  357. $worksheet->write(1, 0, 'Length', $format);
  358. $worksheet->write(1, 1, 25.4);
  359. $worksheet->write(1, 2, 25.4);
  360. $worksheet->write(1, 3, 24.8);
  361. $worksheet->write(1, 4, 25.0);
  362. $worksheet->write(1, 5, 25.3);
  363. $worksheet->write(1, 6, 24.9);
  364. $worksheet->write(1, 7, 25.2);
  365. $worksheet->write(1, 8, 24.8);
  366. # Write some statistical functions
  367. $worksheet->write(4, 0, 'Count', $format);
  368. $worksheet->write(4, 1, '=COUNT(B1:I1)');
  369. $worksheet->write(5, 0, 'Sum', $format);
  370. $worksheet->write(5, 1, '=SUM(B2:I2)');
  371. $worksheet->write(6, 0, 'Average', $format);
  372. $worksheet->write(6, 1, '=AVERAGE(B2:I2)');
  373. $worksheet->write(7, 0, 'Min', $format);
  374. $worksheet->write(7, 1, '=MIN(B2:I2)');
  375. $worksheet->write(8, 0, 'Max', $format);
  376. $worksheet->write(8, 1, '=MAX(B2:I2)');
  377. $worksheet->write(9, 0, 'Standard Deviation', $format);
  378. $worksheet->write(9, 1, '=STDEV(B2:I2)');
  379. $worksheet->write(10, 0, 'Kurtosis', $format);
  380. $worksheet->write(10, 1, '=KURT(B2:I2)');
  381. __END__
  382. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/stats.pl>
  383. =head2 Example: formats.pl
  384. Examples of formatting using the Spreadsheet::WriteExcel module.
  385. This program demonstrates almost all possible formatting options. It is worth
  386. running this program and viewing the output Excel file if you are interested
  387. in the various formatting possibilities.
  388. =begin html
  389. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/formats.jpg" width="640" height="420" alt="Output from formats.pl" /></center></p>
  390. =end html
  391. Source code for this example:
  392. #!/usr/bin/perl -w
  393. ###############################################################################
  394. #
  395. # Examples of formatting using the Spreadsheet::WriteExcel module.
  396. #
  397. # This program demonstrates almost all possible formatting options. It is worth
  398. # running this program and viewing the output Excel file if you are interested
  399. # in the various formatting possibilities.
  400. #
  401. # reverse('©'), September 2002, John McNamara, jmcnamara@cpan.org
  402. #
  403. use strict;
  404. use Spreadsheet::WriteExcel;
  405. my $workbook = Spreadsheet::WriteExcel->new('formats.xls');
  406. # Some common formats
  407. my $center = $workbook->add_format(align => 'center');
  408. my $heading = $workbook->add_format(align => 'center', bold => 1);
  409. # The named colors
  410. my %colors = (
  411. 0x08, 'black',
  412. 0x0C, 'blue',
  413. 0x10, 'brown',
  414. 0x0F, 'cyan',
  415. 0x17, 'gray',
  416. 0x11, 'green',
  417. 0x0B, 'lime',
  418. 0x0E, 'magenta',
  419. 0x12, 'navy',
  420. 0x35, 'orange',
  421. 0x21, 'pink',
  422. 0x14, 'purple',
  423. 0x0A, 'red',
  424. 0x16, 'silver',
  425. 0x09, 'white',
  426. 0x0D, 'yellow',
  427. );
  428. # Call these subroutines to demonstrate different formatting options
  429. intro();
  430. fonts();
  431. named_colors();
  432. standard_colors();
  433. numeric_formats();
  434. borders();
  435. patterns();
  436. alignment();
  437. misc();
  438. # Note: this is required
  439. $workbook->close();
  440. ######################################################################
  441. #
  442. # Intro.
  443. #
  444. sub intro {
  445. my $worksheet = $workbook->add_worksheet('Introduction');
  446. $worksheet->set_column(0, 0, 60);
  447. my $format = $workbook->add_format();
  448. $format->set_bold();
  449. $format->set_size(14);
  450. $format->set_color('blue');
  451. $format->set_align('center');
  452. my $format2 = $workbook->add_format();
  453. $format2->set_bold();
  454. $format2->set_color('blue');
  455. $worksheet->write(2, 0, 'This workbook demonstrates some of', $format);
  456. $worksheet->write(3, 0, 'the formatting options provided by', $format);
  457. $worksheet->write(4, 0, 'the Spreadsheet::WriteExcel module.', $format);
  458. $worksheet->write('A7', 'Sections:', $format2);
  459. $worksheet->write('A8', "internal:Fonts!A1", 'Fonts' );
  460. $worksheet->write('A9', "internal:'Named colors'!A1", 'Named colors' );
  461. $worksheet->write('A10', "internal:'Standard colors'!A1", 'Standard colors');
  462. $worksheet->write('A11', "internal:'Numeric formats'!A1", 'Numeric formats');
  463. $worksheet->write('A12', "internal:Borders!A1", 'Borders' );
  464. $worksheet->write('A13', "internal:Patterns!A1", 'Patterns' );
  465. $worksheet->write('A14', "internal:Alignment!A1", 'Alignment' );
  466. $worksheet->write('A15', "internal:Miscellaneous!A1", 'Miscellaneous' );
  467. }
  468. ######################################################################
  469. #
  470. # Demonstrate the named colors.
  471. #
  472. sub named_colors {
  473. my $worksheet = $workbook->add_worksheet('Named colors');
  474. $worksheet->set_column(0, 3, 15);
  475. $worksheet->write(0, 0, "Index", $heading);
  476. $worksheet->write(0, 1, "Index", $heading);
  477. $worksheet->write(0, 2, "Name", $heading);
  478. $worksheet->write(0, 3, "Color", $heading);
  479. my $i = 1;
  480. while (my($index, $color) = each %colors) {
  481. my $format = $workbook->add_format(
  482. bg_color => $color,
  483. pattern => 1,
  484. border => 1
  485. );
  486. $worksheet->write($i+1, 0, $index, $center);
  487. $worksheet->write($i+1, 1, sprintf("0x%02X", $index), $center);
  488. $worksheet->write($i+1, 2, $color, $center);
  489. $worksheet->write($i+1, 3, '', $format);
  490. $i++;
  491. }
  492. }
  493. ######################################################################
  494. #
  495. # Demonstrate the standard Excel colors in the range 8..63.
  496. #
  497. sub standard_colors {
  498. my $worksheet = $workbook->add_worksheet('Standard colors');
  499. $worksheet->set_column(0, 3, 15);
  500. $worksheet->write(0, 0, "Index", $heading);
  501. $worksheet->write(0, 1, "Index", $heading);
  502. $worksheet->write(0, 2, "Color", $heading);
  503. $worksheet->write(0, 3, "Name", $heading);
  504. for my $i (8..63) {
  505. my $format = $workbook->add_format(
  506. bg_color => $i,
  507. pattern => 1,
  508. border => 1
  509. );
  510. $worksheet->write(($i -7), 0, $i, $center);
  511. $worksheet->write(($i -7), 1, sprintf("0x%02X", $i), $center);
  512. $worksheet->write(($i -7), 2, '', $format);
  513. # Add the color names
  514. if (exists $colors{$i}) {
  515. $worksheet->write(($i -7), 3, $colors{$i}, $center);
  516. }
  517. }
  518. }
  519. ######################################################################
  520. #
  521. # Demonstrate the standard numeric formats.
  522. #
  523. sub numeric_formats {
  524. my $worksheet = $workbook->add_worksheet('Numeric formats');
  525. $worksheet->set_column(0, 4, 15);
  526. $worksheet->set_column(5, 5, 45);
  527. $worksheet->write(0, 0, "Index", $heading);
  528. $worksheet->write(0, 1, "Index", $heading);
  529. $worksheet->write(0, 2, "Unformatted", $heading);
  530. $worksheet->write(0, 3, "Formatted", $heading);
  531. $worksheet->write(0, 4, "Negative", $heading);
  532. $worksheet->write(0, 5, "Format", $heading);
  533. my @formats;
  534. push @formats, [ 0x00, 1234.567, 0, 'General' ];
  535. push @formats, [ 0x01, 1234.567, 0, '0' ];
  536. push @formats, [ 0x02, 1234.567, 0, '0.00' ];
  537. push @formats, [ 0x03, 1234.567, 0, '#,##0' ];
  538. push @formats, [ 0x04, 1234.567, 0, '#,##0.00' ];
  539. push @formats, [ 0x05, 1234.567, -1234.567, '($#,##0_);($#,##0)' ];
  540. push @formats, [ 0x06, 1234.567, -1234.567, '($#,##0_);[Red]($#,##0)' ];
  541. push @formats, [ 0x07, 1234.567, -1234.567, '($#,##0.00_);($#,##0.00)' ];
  542. push @formats, [ 0x08, 1234.567, -1234.567, '($#,##0.00_);[Red]($#,##0.00)' ];
  543. push @formats, [ 0x09, 0.567, 0, '0%' ];
  544. push @formats, [ 0x0a, 0.567, 0, '0.00%' ];
  545. push @formats, [ 0x0b, 1234.567, 0, '0.00E+00' ];
  546. push @formats, [ 0x0c, 0.75, 0, '# ?/?' ];
  547. push @formats, [ 0x0d, 0.3125, 0, '# ??/??' ];
  548. push @formats, [ 0x0e, 36892.521, 0, 'm/d/yy' ];
  549. push @formats, [ 0x0f, 36892.521, 0, 'd-mmm-yy' ];
  550. push @formats, [ 0x10, 36892.521, 0, 'd-mmm' ];
  551. push @formats, [ 0x11, 36892.521, 0, 'mmm-yy' ];
  552. push @formats, [ 0x12, 36892.521, 0, 'h:mm AM/PM' ];
  553. push @formats, [ 0x13, 36892.521, 0, 'h:mm:ss AM/PM' ];
  554. push @formats, [ 0x14, 36892.521, 0, 'h:mm' ];
  555. push @formats, [ 0x15, 36892.521, 0, 'h:mm:ss' ];
  556. push @formats, [ 0x16, 36892.521, 0, 'm/d/yy h:mm' ];
  557. push @formats, [ 0x25, 1234.567, -1234.567, '(#,##0_);(#,##0)' ];
  558. push @formats, [ 0x26, 1234.567, -1234.567, '(#,##0_);[Red](#,##0)' ];
  559. push @formats, [ 0x27, 1234.567, -1234.567, '(#,##0.00_);(#,##0.00)' ];
  560. push @formats, [ 0x28, 1234.567, -1234.567, '(#,##0.00_);[Red](#,##0.00)' ];
  561. push @formats, [ 0x29, 1234.567, -1234.567, '_(* #,##0_);_(* (#,##0);_(* "-"_);_(@_)' ];
  562. push @formats, [ 0x2a, 1234.567, -1234.567, '_($* #,##0_);_($* (#,##0);_($* "-"_);_(@_)' ];
  563. push @formats, [ 0x2b, 1234.567, -1234.567, '_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)' ];
  564. push @formats, [ 0x2c, 1234.567, -1234.567, '_($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_)' ];
  565. push @formats, [ 0x2d, 36892.521, 0, 'mm:ss' ];
  566. push @formats, [ 0x2e, 3.0153, 0, '[h]:mm:ss' ];
  567. push @formats, [ 0x2f, 36892.521, 0, 'mm:ss.0' ];
  568. push @formats, [ 0x30, 1234.567, 0, '##0.0E+0' ];
  569. push @formats, [ 0x31, 1234.567, 0, '@' ];
  570. my $i;
  571. foreach my $format (@formats){
  572. my $style = $workbook->add_format();
  573. $style->set_num_format($format->[0]);
  574. $i++;
  575. $worksheet->write($i, 0, $format->[0], $center);
  576. $worksheet->write($i, 1, sprintf("0x%02X", $format->[0]), $center);
  577. $worksheet->write($i, 2, $format->[1], $center);
  578. $worksheet->write($i, 3, $format->[1], $style);
  579. if ($format->[2]) {
  580. $worksheet->write($i, 4, $format->[2], $style);
  581. }
  582. $worksheet->write_string($i, 5, $format->[3]);
  583. }
  584. }
  585. ######################################################################
  586. #
  587. # Demonstrate the font options.
  588. #
  589. sub fonts {
  590. my $worksheet = $workbook->add_worksheet('Fonts');
  591. $worksheet->set_column(0, 0, 30);
  592. $worksheet->set_column(1, 1, 10);
  593. $worksheet->write(0, 0, "Font name", $heading);
  594. $worksheet->write(0, 1, "Font size", $heading);
  595. my @fonts;
  596. push @fonts, [ 10, 'Arial' ];
  597. push @fonts, [ 12, 'Arial' ];
  598. push @fonts, [ 14, 'Arial' ];
  599. push @fonts, [ 12, 'Arial Black' ];
  600. push @fonts, [ 12, 'Arial Narrow' ];
  601. push @fonts, [ 12, 'Century Schoolbook' ];
  602. push @fonts, [ 12, 'Courier' ];
  603. push @fonts, [ 12, 'Courier New' ];
  604. push @fonts, [ 12, 'Garamond' ];
  605. push @fonts, [ 12, 'Impact' ];
  606. push @fonts, [ 12, 'Lucida Handwriting'] ;
  607. push @fonts, [ 12, 'Times New Roman' ];
  608. push @fonts, [ 12, 'Symbol' ];
  609. push @fonts, [ 12, 'Wingdings' ];
  610. push @fonts, [ 12, 'A font that doesn\'t exist' ];
  611. my $i;
  612. foreach my $font (@fonts){
  613. my $format = $workbook->add_format();
  614. $format->set_size($font->[0]);
  615. $format->set_font($font->[1]);
  616. $i++;
  617. $worksheet->write($i, 0, $font->[1], $format);
  618. $worksheet->write($i, 1, $font->[0], $format);
  619. }
  620. }
  621. ######################################################################
  622. #
  623. # Demonstrate the standard Excel border styles.
  624. #
  625. sub borders {
  626. my $worksheet = $workbook->add_worksheet('Borders');
  627. $worksheet->set_column(0, 4, 10);
  628. $worksheet->set_column(5, 5, 40);
  629. $worksheet->write(0, 0, "Index", $heading);
  630. $worksheet->write(0, 1, "Index", $heading);
  631. $worksheet->write(0, 3, "Style", $heading);
  632. $worksheet->write(0, 5, "The style is highlighted in red for ", $heading);
  633. $worksheet->write(1, 5, "emphasis, the default color is black.", $heading);
  634. for my $i (0..13){
  635. my $format = $workbook->add_format();
  636. $format->set_border($i);
  637. $format->set_border_color('red');
  638. $format->set_align('center');
  639. $worksheet->write((2*($i+1)), 0, $i, $center);
  640. $worksheet->write((2*($i+1)), 1, sprintf("0x%02X", $i), $center);
  641. $worksheet->write((2*($i+1)), 3, "Border", $format);
  642. }
  643. $worksheet->write(30, 0, "Diag type", $heading);
  644. $worksheet->write(30, 1, "Index", $heading);
  645. $worksheet->write(30, 3, "Style", $heading);
  646. $worksheet->write(30, 5, "Diagonal Boder styles", $heading);
  647. for my $i (1..3){
  648. my $format = $workbook->add_format();
  649. $format->set_diag_type($i);
  650. $format->set_diag_border(1);
  651. $format->set_diag_color('red');
  652. $format->set_align('center');
  653. $worksheet->write((2*($i+15)), 0, $i, $center);
  654. $worksheet->write((2*($i+15)), 1, sprintf("0x%02X", $i), $center);
  655. $worksheet->write((2*($i+15)), 3, "Border", $format);
  656. }
  657. }
  658. ######################################################################
  659. #
  660. # Demonstrate the standard Excel cell patterns.
  661. #
  662. sub patterns {
  663. my $worksheet = $workbook->add_worksheet('Patterns');
  664. $worksheet->set_column(0, 4, 10);
  665. $worksheet->set_column(5, 5, 50);
  666. $worksheet->write(0, 0, "Index", $heading);
  667. $worksheet->write(0, 1, "Index", $heading);
  668. $worksheet->write(0, 3, "Pattern", $heading);
  669. $worksheet->write(0, 5, "The background colour has been set to silver.", $heading);
  670. $worksheet->write(1, 5, "The foreground colour has been set to green.", $heading);
  671. for my $i (0..18){
  672. my $format = $workbook->add_format();
  673. $format->set_pattern($i);
  674. $format->set_bg_color('silver');
  675. $format->set_fg_color('green');
  676. $format->set_align('center');
  677. $worksheet->write((2*($i+1)), 0, $i, $center);
  678. $worksheet->write((2*($i+1)), 1, sprintf("0x%02X", $i), $center);
  679. $worksheet->write((2*($i+1)), 3, "Pattern", $format);
  680. if ($i == 1) {
  681. $worksheet->write((2*($i+1)), 5, "This is solid colour, the most useful pattern.", $heading);
  682. }
  683. }
  684. }
  685. ######################################################################
  686. #
  687. # Demonstrate the standard Excel cell alignments.
  688. #
  689. sub alignment {
  690. my $worksheet = $workbook->add_worksheet('Alignment');
  691. $worksheet->set_column(0, 7, 12);
  692. $worksheet->set_row(0, 40);
  693. $worksheet->set_selection(7, 0);
  694. my $format01 = $workbook->add_format();
  695. my $format02 = $workbook->add_format();
  696. my $format03 = $workbook->add_format();
  697. my $format04 = $workbook->add_format();
  698. my $format05 = $workbook->add_format();
  699. my $format06 = $workbook->add_format();
  700. my $format07 = $workbook->add_format();
  701. my $format08 = $workbook->add_format();
  702. my $format09 = $workbook->add_format();
  703. my $format10 = $workbook->add_format();
  704. my $format11 = $workbook->add_format();
  705. my $format12 = $workbook->add_format();
  706. my $format13 = $workbook->add_format();
  707. my $format14 = $workbook->add_format();
  708. my $format15 = $workbook->add_format();
  709. my $format16 = $workbook->add_format();
  710. my $format17 = $workbook->add_format();
  711. $format02->set_align('top');
  712. $format03->set_align('bottom');
  713. $format04->set_align('vcenter');
  714. $format05->set_align('vjustify');
  715. $format06->set_text_wrap();
  716. $format07->set_align('left');
  717. $format08->set_align('right');
  718. $format09->set_align('center');
  719. $format10->set_align('fill');
  720. $format11->set_align('justify');
  721. $format12->set_merge();
  722. $format13->set_rotation(45);
  723. $format14->set_rotation(-45);
  724. $format15->set_rotation(270);
  725. $format16->set_shrink();
  726. $format17->set_indent(1);
  727. $worksheet->write(0, 0, 'Vertical', $heading);
  728. $worksheet->write(0, 1, 'top', $format02);
  729. $worksheet->write(0, 2, 'bottom', $format03);
  730. $worksheet->write(0, 3, 'vcenter', $format04);
  731. $worksheet->write(0, 4, 'vjustify', $format05);
  732. $worksheet->write(0, 5, "text\nwrap", $format06);
  733. $worksheet->write(2, 0, 'Horizontal', $heading);
  734. $worksheet->write(2, 1, 'left', $format07);
  735. $worksheet->write(2, 2, 'right', $format08);
  736. $worksheet->write(2, 3, 'center', $format09);
  737. $worksheet->write(2, 4, 'fill', $format10);
  738. $worksheet->write(2, 5, 'justify', $format11);
  739. $worksheet->write(3, 1, 'merge', $format12);
  740. $worksheet->write(3, 2, '', $format12);
  741. $worksheet->write(3, 3, 'Shrink ' x 3, $format16);
  742. $worksheet->write(3, 4, 'Indent', $format17);
  743. $worksheet->write(5, 0, 'Rotation', $heading);
  744. $worksheet->write(5, 1, 'Rotate 45', $format13);
  745. $worksheet->write(6, 1, 'Rotate -45', $format14);
  746. $worksheet->write(7, 1, 'Rotate 270', $format15);
  747. }
  748. ######################################################################
  749. #
  750. # Demonstrate other miscellaneous features.
  751. #
  752. sub misc {
  753. my $worksheet = $workbook->add_worksheet('Miscellaneous');
  754. $worksheet->set_column(2, 2, 25);
  755. my $format01 = $workbook->add_format();
  756. my $format02 = $workbook->add_format();
  757. my $format03 = $workbook->add_format();
  758. my $format04 = $workbook->add_format();
  759. my $format05 = $workbook->add_format();
  760. my $format06 = $workbook->add_format();
  761. my $format07 = $workbook->add_format();
  762. $format01->set_underline(0x01);
  763. $format02->set_underline(0x02);
  764. $format03->set_underline(0x21);
  765. $format04->set_underline(0x22);
  766. $format05->set_font_strikeout();
  767. $format06->set_font_outline();
  768. $format07->set_font_shadow();
  769. $worksheet->write(1, 2, 'Underline 0x01', $format01);
  770. $worksheet->write(3, 2, 'Underline 0x02', $format02);
  771. $worksheet->write(5, 2, 'Underline 0x21', $format03);
  772. $worksheet->write(7, 2, 'Underline 0x22', $format04);
  773. $worksheet->write(9, 2, 'Strikeout', $format05);
  774. $worksheet->write(11, 2, 'Outline (Macintosh only)', $format06);
  775. $worksheet->write(13, 2, 'Shadow (Macintosh only)', $format07);
  776. }
  777. __END__
  778. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/formats.pl>
  779. =head2 Example: bug_report.pl
  780. A template for submitting a bug report.
  781. Run this program and read the output from the command line.
  782. #!/usr/bin/perl -w
  783. ###############################################################################
  784. #
  785. # A template for submitting a bug report.
  786. #
  787. # Run this program and read the output from the command line.
  788. #
  789. # reverse('©'), March 2004, John McNamara, jmcnamara@cpan.org
  790. #
  791. use strict;
  792. print << 'HINTS_1';
  793. REPORTING A BUG OR ASKING A QUESTION
  794. Feel free to report bugs or ask questions. However, to save time
  795. consider the following steps first:
  796. Read the documentation:
  797. The Spreadsheet::WriteExcel documentation has been refined in
  798. response to user questions. Therefore, if you have a question it is
  799. possible that someone else has asked it before you and that it is
  800. already addressed in the documentation. Since there is a lot of
  801. documentation to get through you should at least read the table of
  802. contents and search for keywords that you are interested in.
  803. Look at the example programs:
  804. There are over 70 example programs shipped with the standard
  805. Spreadsheet::WriteExcel distribution. Many of these were created in
  806. response to user questions. Try to identify an example program that
  807. corresponds to your query and adapt it to your needs.
  808. HINTS_1
  809. print "Press enter ..."; <STDIN>;
  810. print << 'HINTS_2';
  811. If you submit a bug report here are some pointers.
  812. 1. Put "WriteExcel:" at the beginning of the subject line. This helps
  813. to filter genuine messages from spam.
  814. 2. Describe the problems as clearly and as concisely as possible.
  815. 3. Send a sample program. It is often easier to describe a problem in
  816. code than in written prose.
  817. 4. The sample program should be as small as possible to demonstrate the
  818. problem. Don't copy and past large sections of your program. The
  819. program should also be self contained and working.
  820. A sample bug report is generated below. If you use this format then it
  821. will help to analyse your question and respond to it more quickly.
  822. Please don't send patches without contacting the author first.
  823. HINTS_2
  824. print "Press enter ..."; <STDIN>;
  825. print << 'EMAIL';
  826. =======================================================================
  827. To: John McNamara <jmcnamara@cpan.org>
  828. Subject: WriteExcel: Problem with something.
  829. Hi John,
  830. I am using Spreadsheet::WriteExcel and I have encountered a problem. I
  831. want it to do SOMETHING but the module appears to do SOMETHING_ELSE.
  832. Here is some code that demonstrates the problem.
  833. #!/usr/bin/perl -w
  834. use strict;
  835. use Spreadsheet::WriteExcel;
  836. my $workbook = Spreadsheet::WriteExcel->new("reload.xls");
  837. my $worksheet = $workbook->add_worksheet();
  838. $worksheet->write(0, 0, "Hi Excel!");
  839. __END__
  840. I tested using Excel XX (or Gnumeric or OpenOffice.org).
  841. My automatically generated system details are as follows:
  842. EMAIL
  843. print "\n Perl version : $]";
  844. print "\n OS name : $^O";
  845. print "\n Module versions: (not all are required)\n";
  846. my @modules = qw(
  847. Spreadsheet::WriteExcel
  848. Spreadsheet::ParseExcel
  849. OLE::Storage_Lite
  850. Parse::RecDescent
  851. File::Temp
  852. Digest::MD4
  853. Digest::Perl::MD4
  854. Digest::MD5
  855. );
  856. for my $module (@modules) {
  857. my $version;
  858. eval "require $module";
  859. if (not $@) {
  860. $version = $module->VERSION;
  861. $version = '(unknown)' if not defined $version;
  862. }
  863. else {
  864. $version = '(not installed)';
  865. }
  866. printf "%21s%-24s\t%s\n", "", $module, $version;
  867. }
  868. print << "BYE";
  869. Yours etc.,
  870. A. Person
  871. --
  872. BYE
  873. __END__
  874. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/bug_report.pl>
  875. =head2 Example: autofilter.pl
  876. An example of how to create autofilters with Spreadsheet::WriteExcel.
  877. An autofilter is a way of adding drop down lists to the headers of a 2D range
  878. of worksheet data. This is turn allow users to filter the data based on
  879. simple criteria so that some data is shown and some is hidden.
  880. =begin html
  881. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/autofilter.jpg" width="640" height="420" alt="Output from autofilter.pl" /></center></p>
  882. =end html
  883. Source code for this example:
  884. #!/usr/bin/perl -w
  885. ###############################################################################
  886. #
  887. # An example of how to create autofilters with Spreadsheet::WriteExcel.
  888. #
  889. # An autofilter is a way of adding drop down lists to the headers of a 2D range
  890. # of worksheet data. This is turn allow users to filter the data based on
  891. # simple criteria so that some data is shown and some is hidden.
  892. #
  893. # reverse('©'), September 2007, John McNamara, jmcnamara@cpan.org
  894. #
  895. use strict;
  896. use Spreadsheet::WriteExcel;
  897. my $workbook = Spreadsheet::WriteExcel->new('autofilter.xls');
  898. die "Couldn't create new Excel file: $!.\n" unless defined $workbook;
  899. my $worksheet1 = $workbook->add_worksheet();
  900. my $worksheet2 = $workbook->add_worksheet();
  901. my $worksheet3 = $workbook->add_worksheet();
  902. my $worksheet4 = $workbook->add_worksheet();
  903. my $worksheet5 = $workbook->add_worksheet();
  904. my $worksheet6 = $workbook->add_worksheet();
  905. my $bold = $workbook->add_format(bold => 1);
  906. # Extract the data embedded at the end of this file.
  907. my @headings = split ' ', <DATA>;
  908. my @data;
  909. push @data, [split] while <DATA>;
  910. # Set up several sheets with the same data.
  911. for my $worksheet ($workbook->sheets()) {
  912. $worksheet->set_column('A:D', 12);
  913. $worksheet->set_row(0, 20, $bold);
  914. $worksheet->write('A1', \@headings);
  915. }
  916. ###############################################################################
  917. #
  918. # Example 1. Autofilter without conditions.
  919. #
  920. $worksheet1->autofilter('A1:D51');
  921. $worksheet1->write('A2', [[@data]]);
  922. ###############################################################################
  923. #
  924. #
  925. # Example 2. Autofilter with a filter condition in the first column.
  926. #
  927. # The range in this example is the same as above but in row-column notation.
  928. $worksheet2->autofilter(0, 0, 50, 3);
  929. # The placeholder "Region" in the filter is ignored and can be any string
  930. # that adds clarity to the expression.
  931. #
  932. $worksheet2->filter_column(0, 'Region eq East');
  933. #
  934. # Hide the rows that don't match the filter criteria.
  935. #
  936. my $row = 1;
  937. for my $row_data (@data) {
  938. my $region = $row_data->[0];
  939. if ($region eq 'East') {
  940. # Row is visible.
  941. }
  942. else {
  943. # Hide row.
  944. $worksheet2->set_row($row, undef, undef, 1);
  945. }
  946. $worksheet2->write($row++, 0, $row_data);
  947. }
  948. ###############################################################################
  949. #
  950. #
  951. # Example 3. Autofilter with a dual filter condition in one of the columns.
  952. #
  953. $worksheet3->autofilter('A1:D51');
  954. $worksheet3->filter_column('A', 'x eq East or x eq South');
  955. #
  956. # Hide the rows that don't match the filter criteria.
  957. #
  958. $row = 1;
  959. for my $row_data (@data) {
  960. my $region = $row_data->[0];
  961. if ($region eq 'East' or $region eq 'South') {
  962. # Row is visible.
  963. }
  964. else {
  965. # Hide row.
  966. $worksheet3->set_row($row, undef, undef, 1);
  967. }
  968. $worksheet3->write($row++, 0, $row_data);
  969. }
  970. ###############################################################################
  971. #
  972. #
  973. # Example 4. Autofilter with filter conditions in two columns.
  974. #
  975. $worksheet4->autofilter('A1:D51');
  976. $worksheet4->filter_column('A', 'x eq East');
  977. $worksheet4->filter_column('C', 'x > 3000 and x < 8000' );
  978. #
  979. # Hide the rows that don't match the filter criteria.
  980. #
  981. $row = 1;
  982. for my $row_data (@data) {
  983. my $region = $row_data->[0];
  984. my $volume = $row_data->[2];
  985. if ($region eq 'East' and
  986. $volume > 3000 and $volume < 8000
  987. )
  988. {
  989. # Row is visible.
  990. }
  991. else {
  992. # Hide row.
  993. $worksheet4->set_row($row, undef, undef, 1);
  994. }
  995. $worksheet4->write($row++, 0, $row_data);
  996. }
  997. ###############################################################################
  998. #
  999. #
  1000. # Example 5. Autofilter with filter for blanks.
  1001. #
  1002. # Create a blank cell in our test data.
  1003. $data[5]->[0] = '';
  1004. $worksheet5->autofilter('A1:D51');
  1005. $worksheet5->filter_column('A', 'x == Blanks');
  1006. #
  1007. # Hide the rows that don't match the filter criteria.
  1008. #
  1009. $row = 1;
  1010. for my $row_data (@data) {
  1011. my $region = $row_data->[0];
  1012. if ($region eq '')
  1013. {
  1014. # Row is visible.
  1015. }
  1016. else {
  1017. # Hide row.
  1018. $worksheet5->set_row($row, undef, undef, 1);
  1019. }
  1020. $worksheet5->write($row++, 0, $row_data);
  1021. }
  1022. ###############################################################################
  1023. #
  1024. #
  1025. # Example 6. Autofilter with filter for non-blanks.
  1026. #
  1027. $worksheet6->autofilter('A1:D51');
  1028. $worksheet6->filter_column('A', 'x == NonBlanks');
  1029. #
  1030. # Hide the rows that don't match the filter criteria.
  1031. #
  1032. $row = 1;
  1033. for my $row_data (@data) {
  1034. my $region = $row_data->[0];
  1035. if ($region ne '')
  1036. {
  1037. # Row is visible.
  1038. }
  1039. else {
  1040. # Hide row.
  1041. $worksheet6->set_row($row, undef, undef, 1);
  1042. }
  1043. $worksheet6->write($row++, 0, $row_data);
  1044. }
  1045. __DATA__
  1046. Region Item Volume Month
  1047. East Apple 9000 July
  1048. East Apple 5000 July
  1049. South Orange 9000 September
  1050. North Apple 2000 November
  1051. West Apple 9000 November
  1052. South Pear 7000 October
  1053. North Pear 9000 August
  1054. West Orange 1000 December
  1055. West Grape 1000 November
  1056. South Pear 10000 April
  1057. West Grape 6000 January
  1058. South Orange 3000 May
  1059. North Apple 3000 December
  1060. South Apple 7000 February
  1061. West Grape 1000 December
  1062. East Grape 8000 February
  1063. South Grape 10000 June
  1064. West Pear 7000 December
  1065. South Apple 2000 October
  1066. East Grape 7000 December
  1067. North Grape 6000 April
  1068. East Pear 8000 February
  1069. North Apple 7000 August
  1070. North Orange 7000 July
  1071. North Apple 6000 June
  1072. South Grape 8000 September
  1073. West Apple 3000 October
  1074. South Orange 10000 November
  1075. West Grape 4000 July
  1076. North Orange 5000 August
  1077. East Orange 1000 November
  1078. East Orange 4000 October
  1079. North Grape 5000 August
  1080. East Apple 1000 December
  1081. South Apple 10000 March
  1082. East Grape 7000 October
  1083. West Grape 1000 September
  1084. East Grape 10000 October
  1085. South Orange 8000 March
  1086. North Apple 4000 July
  1087. South Orange 5000 July
  1088. West Apple 4000 June
  1089. East Apple 5000 April
  1090. North Pear 3000 August
  1091. East Grape 9000 November
  1092. North Orange 8000 October
  1093. East Apple 10000 June
  1094. South Pear 1000 December
  1095. North Grape 10000 July
  1096. East Grape 6000 February
  1097. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/autofilter.pl>
  1098. =head2 Example: autofit.pl
  1099. Simulate Excel's autofit for column widths.
  1100. Excel provides a function called Autofit (Format->Columns->Autofit) that
  1101. adjusts column widths to match the length of the longest string in a column.
  1102. Excel calculates these widths at run time when it has access to information
  1103. about string lengths and font information. This function is *not* a feature
  1104. of the file format and thus cannot be implemented by Spreadsheet::WriteExcel.
  1105. However, we can make an attempt to simulate it by keeping track of the
  1106. longest string written to each column and then adjusting the column widths
  1107. prior to closing the file.
  1108. We keep track of the longest strings by adding a handler to the write()
  1109. function. See add_handler() in the S::WE docs for more information.
  1110. The main problem with trying to simulate Autofit lies in defining a
  1111. relationship between a string length and its width in a arbitrary font and
  1112. size. We use two approaches below. The first is a simple direct relationship
  1113. obtained by trial and error. The second is a slightly more sophisticated
  1114. method using an external module. For more complicated applications you will
  1115. probably have to work out your own methods.
  1116. =begin html
  1117. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/autofit.jpg" width="640" height="420" alt="Output from autofit.pl" /></center></p>
  1118. =end html
  1119. Source code for this example:
  1120. #!/usr/bin/perl -w
  1121. ##############################################################################
  1122. #
  1123. # Simulate Excel's autofit for column widths.
  1124. #
  1125. # Excel provides a function called Autofit (Format->Columns->Autofit) that
  1126. # adjusts column widths to match the length of the longest string in a column.
  1127. # Excel calculates these widths at run time when it has access to information
  1128. # about string lengths and font information. This function is *not* a feature
  1129. # of the file format and thus cannot be implemented by Spreadsheet::WriteExcel.
  1130. #
  1131. # However, we can make an attempt to simulate it by keeping track of the
  1132. # longest string written to each column and then adjusting the column widths
  1133. # prior to closing the file.
  1134. #
  1135. # We keep track of the longest strings by adding a handler to the write()
  1136. # function. See add_handler() in the S::WE docs for more information.
  1137. #
  1138. # The main problem with trying to simulate Autofit lies in defining a
  1139. # relationship between a string length and its width in a arbitrary font and
  1140. # size. We use two approaches below. The first is a simple direct relationship
  1141. # obtained by trial and error. The second is a slightly more sophisticated
  1142. # method using an external module. For more complicated applications you will
  1143. # probably have to work out your own methods.
  1144. #
  1145. # reverse('©'), May 2006, John McNamara, jmcnamara@cpan.org
  1146. #
  1147. use strict;
  1148. use Spreadsheet::WriteExcel;
  1149. my $workbook = Spreadsheet::WriteExcel->new('autofit.xls');
  1150. my $worksheet = $workbook->add_worksheet();
  1151. ###############################################################################
  1152. #
  1153. # Add a handler to store the width of the longest string written to a column.
  1154. # We use the stored width to simulate an autofit of the column widths.
  1155. #
  1156. # You should do this for every worksheet you want to autofit.
  1157. #
  1158. $worksheet->add_write_handler(qr[\w], \&store_string_widths);
  1159. $worksheet->write('A1', 'Hello');
  1160. $worksheet->write('B1', 'Hello World');
  1161. $worksheet->write('D1', 'Hello');
  1162. $worksheet->write('F1', 'This is a long string as an example.');
  1163. # Run the autofit after you have finished writing strings to the workbook.
  1164. autofit_columns($worksheet);
  1165. ###############################################################################
  1166. #
  1167. # Functions used for Autofit.
  1168. #
  1169. ###############################################################################
  1170. ###############################################################################
  1171. #
  1172. # Adjust the column widths to fit the longest string in the column.
  1173. #
  1174. sub autofit_columns {
  1175. my $worksheet = shift;
  1176. my $col = 0;
  1177. for my $width (@{$worksheet->{__col_widths}}) {
  1178. $worksheet->set_column($col, $col, $width) if $width;
  1179. $col++;
  1180. }
  1181. }
  1182. ###############################################################################
  1183. #
  1184. # The following function is a callback that was added via add_write_handler()
  1185. # above. It modifies the write() function so that it stores the maximum
  1186. # unwrapped width of a string in a column.
  1187. #
  1188. sub store_string_widths {
  1189. my $worksheet = shift;
  1190. my $col = $_[1];
  1191. my $token = $_[2];
  1192. # Ignore some tokens that we aren't interested in.
  1193. return if not defined $token; # Ignore undefs.
  1194. return if $token eq ''; # Ignore blank cells.
  1195. return if ref $token eq 'ARRAY'; # Ignore array refs.
  1196. return if $token =~ /^=/; # Ignore formula
  1197. # Ignore numbers
  1198. return if $token =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;
  1199. # Ignore various internal and external hyperlinks. In a real scenario
  1200. # you may wish to track the length of the optional strings used with
  1201. # urls.
  1202. return if $token =~ m{^[fh]tt?ps?://};
  1203. return if $token =~ m{^mailto:};
  1204. return if $token =~ m{^(?:in|ex)ternal:};
  1205. # We store the string width as data in the Worksheet object. We use
  1206. # a double underscore key name to avoid conflicts with future names.
  1207. #
  1208. my $old_width = $worksheet->{__col_widths}->[$col];
  1209. my $string_width = string_width($token);
  1210. if (not defined $old_width or $string_width > $old_width) {
  1211. # You may wish to set a minimum column width as follows.
  1212. #return undef if $string_width < 10;
  1213. $worksheet->{__col_widths}->[$col] = $string_width;
  1214. }
  1215. # Return control to write();
  1216. return undef;
  1217. }
  1218. ###############################################################################
  1219. #
  1220. # Very simple conversion between string length and string width for Arial 10.
  1221. # See below for a more sophisticated method.
  1222. #
  1223. sub string_width {
  1224. return 0.9 * length $_[0];
  1225. }
  1226. __END__
  1227. ###############################################################################
  1228. #
  1229. # This function uses an external module to get a more accurate width for a
  1230. # string. Note that in a real program you could "use" the module instead of
  1231. # "require"-ing it and you could make the Font object global to avoid repeated
  1232. # initialisation.
  1233. #
  1234. # Note also that the $pixel_width to $cell_width is specific to Arial. For
  1235. # other fonts you should calculate appropriate relationships. A future version
  1236. # of S::WE will provide a way of specifying column widths in pixels instead of
  1237. # cell units in order to simplify this conversion.
  1238. #
  1239. sub string_width {
  1240. require Font::TTFMetrics;
  1241. my $arial = Font::TTFMetrics->new('c:\windows\fonts\arial.ttf');
  1242. my $font_size = 10;
  1243. my $dpi = 96;
  1244. my $units_per_em = $arial->get_units_per_em();
  1245. my $font_width = $arial->string_width($_[0]);
  1246. # Convert to pixels as per TTFMetrics docs.
  1247. my $pixel_width = 6 + $font_width *$font_size *$dpi /(72 *$units_per_em);
  1248. # Add extra pixels for border around text.
  1249. $pixel_width += 6;
  1250. # Convert to cell width (for Arial) and for cell widths > 1.
  1251. my $cell_width = ($pixel_width -5) /7;
  1252. return $cell_width;
  1253. }
  1254. __END__
  1255. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/autofit.pl>
  1256. =head2 Example: bigfile.pl
  1257. Example of creating a Spreadsheet::WriteExcel that is larger than the
  1258. default 7MB limit.
  1259. This is exactly that same as any other Spreadsheet::WriteExcel program except
  1260. that is requires that the OLE::Storage module is installed.
  1261. =begin html
  1262. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/bigfile.jpg" width="640" height="420" alt="Output from bigfile.pl" /></center></p>
  1263. =end html
  1264. Source code for this example:
  1265. #!/usr/bin/perl -w
  1266. ###############################################################################
  1267. #
  1268. # Example of creating a Spreadsheet::WriteExcel that is larger than the
  1269. # default 7MB limit.
  1270. #
  1271. # This is exactly that same as any other Spreadsheet::WriteExcel program except
  1272. # that is requires that the OLE::Storage module is installed.
  1273. #
  1274. # reverse('©'), Jan 2007, John McNamara, jmcnamara@cpan.org
  1275. use strict;
  1276. use Spreadsheet::WriteExcel;
  1277. my $workbook = Spreadsheet::WriteExcel->new('bigfile.xls');
  1278. my $worksheet = $workbook->add_worksheet();
  1279. $worksheet->set_column(0, 50, 18);
  1280. for my $col (0 .. 50) {
  1281. for my $row (0 .. 6000) {
  1282. $worksheet->write($row, $col, "Row: $row Col: $col");
  1283. }
  1284. }
  1285. __END__
  1286. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/bigfile.pl>
  1287. =head2 Example: cgi.pl
  1288. Example of how to use the Spreadsheet::WriteExcel module to send an Excel
  1289. file to a browser in a CGI program.
  1290. On Windows the hash-bang line should be something like:
  1291. #!C:\Perl\bin\perl.exe
  1292. The "Content-Disposition" line will cause a prompt to be generated to save
  1293. the file. If you want to stream the file to the browser instead, comment out
  1294. that line as shown below.
  1295. #!/usr/bin/perl -w
  1296. ###############################################################################
  1297. #
  1298. # Example of how to use the Spreadsheet::WriteExcel module to send an Excel
  1299. # file to a browser in a CGI program.
  1300. #
  1301. # On Windows the hash-bang line should be something like:
  1302. #
  1303. # #!C:\Perl\bin\perl.exe
  1304. #
  1305. # The "Content-Disposition" line will cause a prompt to be generated to save
  1306. # the file. If you want to stream the file to the browser instead, comment out
  1307. # that line as shown below.
  1308. #
  1309. # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org
  1310. #
  1311. use strict;
  1312. use Spreadsheet::WriteExcel;
  1313. # Set the filename and send the content type
  1314. my $filename ="cgitest.xls";
  1315. print "Content-type: application/vnd.ms-excel\n";
  1316. # The Content-Disposition will generate a prompt to save the file. If you want
  1317. # to stream the file to the browser, comment out the following line.
  1318. print "Content-Disposition: attachment; filename=$filename\n";
  1319. print "\n";
  1320. # Create a new workbook and add a worksheet. The special Perl filehandle - will
  1321. # redirect the output to STDOUT
  1322. #
  1323. my $workbook = Spreadsheet::WriteExcel->new("-");
  1324. my $worksheet = $workbook->add_worksheet();
  1325. # Set the column width for column 1
  1326. $worksheet->set_column(0, 0, 20);
  1327. # Create a format
  1328. my $format = $workbook->add_format();
  1329. $format->set_bold();
  1330. $format->set_size(15);
  1331. $format->set_color('blue');
  1332. # Write to the workbook
  1333. $worksheet->write(0, 0, "Hi Excel!", $format);
  1334. __END__
  1335. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/cgi.pl>
  1336. =head2 Example: chart_area.pl
  1337. A simple demo of Area charts in Spreadsheet::WriteExcel.
  1338. =begin html
  1339. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/chart_area.jpg" width="640" height="420" alt="Output from chart_area.pl" /></center></p>
  1340. =end html
  1341. Source code for this example:
  1342. #!/usr/bin/perl -w
  1343. ###############################################################################
  1344. #
  1345. # A simple demo of Area charts in Spreadsheet::WriteExcel.
  1346. #
  1347. # reverse('©'), December 2009, John McNamara, jmcnamara@cpan.org
  1348. #
  1349. use strict;
  1350. use Spreadsheet::WriteExcel;
  1351. my $workbook = Spreadsheet::WriteExcel->new( 'chart_area.xls' );
  1352. my $worksheet = $workbook->add_worksheet();
  1353. my $bold = $workbook->add_format( bold => 1 );
  1354. # Add the worksheet data that the charts will refer to.
  1355. my $headings = [ 'Category', 'Values 1', 'Values 2' ];
  1356. my $data = [
  1357. [ 2, 3, 4, 5, 6, 7 ],
  1358. [ 1, 4, 5, 2, 1, 5 ],
  1359. [ 3, 6, 7, 5, 4, 3 ],
  1360. ];
  1361. $worksheet->write( 'A1', $headings, $bold );
  1362. $worksheet->write( 'A2', $data );
  1363. ###############################################################################
  1364. #
  1365. # Example 1. A minimal chart.
  1366. #
  1367. my $chart1 = $workbook->add_chart( type => 'area' );
  1368. # Add values only. Use the default categories.
  1369. $chart1->add_series( values => '=Sheet1!$B$2:$B$7' );
  1370. ###############################################################################
  1371. #
  1372. # Example 2. A minimal chart with user specified categories (X axis)
  1373. # and a series name.
  1374. #
  1375. my $chart2 = $workbook->add_chart( type => 'area' );
  1376. # Configure the series.
  1377. $chart2->add_series(
  1378. categories => '=Sheet1!$A$2:$A$7',
  1379. values => '=Sheet1!$B$2:$B$7',
  1380. name => 'Test data series 1',
  1381. );
  1382. ###############################################################################
  1383. #
  1384. # Example 3. Same as previous chart but with added title and axes labels.
  1385. #
  1386. my $chart3 = $workbook->add_chart( type => 'area' );
  1387. # Configure the series.
  1388. $chart3->add_series(
  1389. categories => '=Sheet1!$A$2:$A$7',
  1390. values => '=Sheet1!$B$2:$B$7',
  1391. name => 'Test data series 1',
  1392. );
  1393. # Add some labels.
  1394. $chart3->set_title( name => 'Results of sample analysis' );
  1395. $chart3->set_x_axis( name => 'Sample number' );
  1396. $chart3->set_y_axis( name => 'Sample length (cm)' );
  1397. ###############################################################################
  1398. #
  1399. # Example 4. Same as previous chart but with an added series and with a
  1400. # user specified chart sheet name.
  1401. #
  1402. my $chart4 = $workbook->add_chart( name => 'Results Chart', type => 'area' );
  1403. # Configure the series.
  1404. $chart4->add_series(
  1405. categories => '=Sheet1!$A$2:$A$7',
  1406. values => '=Sheet1!$B$2:$B$7',
  1407. name => 'Test data series 1',
  1408. );
  1409. # Add another series.
  1410. $chart4->add_series(
  1411. categories => '=Sheet1!$A$2:$A$7',
  1412. values => '=Sheet1!$C$2:$C$7',
  1413. name => 'Test data series 2',
  1414. );
  1415. # Add some labels.
  1416. $chart4->set_title( name => 'Results of sample analysis' );
  1417. $chart4->set_x_axis( name => 'Sample number' );
  1418. $chart4->set_y_axis( name => 'Sample length (cm)' );
  1419. ###############################################################################
  1420. #
  1421. # Example 5. Same as Example 3 but as an embedded chart.
  1422. #
  1423. my $chart5 = $workbook->add_chart( type => 'area', embedded => 1 );
  1424. # Configure the series.
  1425. $chart5->add_series(
  1426. categories => '=Sheet1!$A$2:$A$7',
  1427. values => '=Sheet1!$B$2:$B$7',
  1428. name => 'Test data series 1',
  1429. );
  1430. # Add some labels.
  1431. $chart5->set_title( name => 'Results of sample analysis' );
  1432. $chart5->set_x_axis( name => 'Sample number' );
  1433. $chart5->set_y_axis( name => 'Sample length (cm)' );
  1434. # Insert the chart into the main worksheet.
  1435. $worksheet->insert_chart( 'E2', $chart5 );
  1436. __END__
  1437. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/chart_area.pl>
  1438. =head2 Example: chart_bar.pl
  1439. A simple demo of Bar charts in Spreadsheet::WriteExcel.
  1440. =begin html
  1441. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/chart_bar.jpg" width="640" height="420" alt="Output from chart_bar.pl" /></center></p>
  1442. =end html
  1443. Source code for this example:
  1444. #!/usr/bin/perl -w
  1445. ###############################################################################
  1446. #
  1447. # A simple demo of Bar charts in Spreadsheet::WriteExcel.
  1448. #
  1449. # reverse('©'), December 2009, John McNamara, jmcnamara@cpan.org
  1450. #
  1451. use strict;
  1452. use Spreadsheet::WriteExcel;
  1453. my $workbook = Spreadsheet::WriteExcel->new( 'chart_bar.xls' );
  1454. my $worksheet = $workbook->add_worksheet();
  1455. my $bold = $workbook->add_format( bold => 1 );
  1456. # Add the worksheet data that the charts will refer to.
  1457. my $headings = [ 'Category', 'Values 1', 'Values 2' ];
  1458. my $data = [
  1459. [ 2, 3, 4, 5, 6, 7 ],
  1460. [ 1, 4, 5, 2, 1, 5 ],
  1461. [ 3, 6, 7, 5, 4, 3 ],
  1462. ];
  1463. $worksheet->write( 'A1', $headings, $bold );
  1464. $worksheet->write( 'A2', $data );
  1465. ###############################################################################
  1466. #
  1467. # Example 1. A minimal chart.
  1468. #
  1469. my $chart1 = $workbook->add_chart( type => 'bar' );
  1470. # Add values only. Use the default categories.
  1471. $chart1->add_series( values => '=Sheet1!$B$2:$B$7' );
  1472. ###############################################################################
  1473. #
  1474. # Example 2. A minimal chart with user specified categories (X axis)
  1475. # and a series name.
  1476. #
  1477. my $chart2 = $workbook->add_chart( type => 'bar' );
  1478. # Configure the series.
  1479. $chart2->add_series(
  1480. categories => '=Sheet1!$A$2:$A$7',
  1481. values => '=Sheet1!$B$2:$B$7',
  1482. name => 'Test data series 1',
  1483. );
  1484. ###############################################################################
  1485. #
  1486. # Example 3. Same as previous chart but with added title and axes labels.
  1487. #
  1488. my $chart3 = $workbook->add_chart( type => 'bar' );
  1489. # Configure the series.
  1490. $chart3->add_series(
  1491. categories => '=Sheet1!$A$2:$A$7',
  1492. values => '=Sheet1!$B$2:$B$7',
  1493. name => 'Test data series 1',
  1494. );
  1495. # Add some labels.
  1496. $chart3->set_title( name => 'Results of sample analysis' );
  1497. $chart3->set_x_axis( name => 'Sample number' );
  1498. $chart3->set_y_axis( name => 'Sample length (cm)' );
  1499. ###############################################################################
  1500. #
  1501. # Example 4. Same as previous chart but with an added series and with a
  1502. # user specified chart sheet name.
  1503. #
  1504. my $chart4 = $workbook->add_chart( name => 'Results Chart', type => 'bar' );
  1505. # Configure the series.
  1506. $chart4->add_series(
  1507. categories => '=Sheet1!$A$2:$A$7',
  1508. values => '=Sheet1!$B$2:$B$7',
  1509. name => 'Test data series 1',
  1510. );
  1511. # Add another series.
  1512. $chart4->add_series(
  1513. categories => '=Sheet1!$A$2:$A$7',
  1514. values => '=Sheet1!$C$2:$C$7',
  1515. name => 'Test data series 2',
  1516. );
  1517. # Add some labels.
  1518. $chart4->set_title( name => 'Results of sample analysis' );
  1519. $chart4->set_x_axis( name => 'Sample number' );
  1520. $chart4->set_y_axis( name => 'Sample length (cm)' );
  1521. ###############################################################################
  1522. #
  1523. # Example 5. Same as Example 3 but as an embedded chart.
  1524. #
  1525. my $chart5 = $workbook->add_chart( type => 'bar', embedded => 1 );
  1526. # Configure the series.
  1527. $chart5->add_series(
  1528. categories => '=Sheet1!$A$2:$A$7',
  1529. values => '=Sheet1!$B$2:$B$7',
  1530. name => 'Test data series 1',
  1531. );
  1532. # Add some labels.
  1533. $chart5->set_title( name => 'Results of sample analysis' );
  1534. $chart5->set_x_axis( name => 'Sample number' );
  1535. $chart5->set_y_axis( name => 'Sample length (cm)' );
  1536. # Insert the chart into the main worksheet.
  1537. $worksheet->insert_chart( 'E2', $chart5 );
  1538. __END__
  1539. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/chart_bar.pl>
  1540. =head2 Example: chart_column.pl
  1541. A simple demo of Column charts in Spreadsheet::WriteExcel.
  1542. =begin html
  1543. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/chart_column.jpg" width="640" height="420" alt="Output from chart_column.pl" /></center></p>
  1544. =end html
  1545. Source code for this example:
  1546. #!/usr/bin/perl -w
  1547. ###############################################################################
  1548. #
  1549. # A simple demo of Column charts in Spreadsheet::WriteExcel.
  1550. #
  1551. # reverse('©'), December 2009, John McNamara, jmcnamara@cpan.org
  1552. #
  1553. use strict;
  1554. use Spreadsheet::WriteExcel;
  1555. my $workbook = Spreadsheet::WriteExcel->new( 'chart_column.xls' );
  1556. my $worksheet = $workbook->add_worksheet();
  1557. my $bold = $workbook->add_format( bold => 1 );
  1558. # Add the worksheet data that the charts will refer to.
  1559. my $headings = [ 'Category', 'Values 1', 'Values 2' ];
  1560. my $data = [
  1561. [ 2, 3, 4, 5, 6, 7 ],
  1562. [ 1, 4, 5, 2, 1, 5 ],
  1563. [ 3, 6, 7, 5, 4, 3 ],
  1564. ];
  1565. $worksheet->write( 'A1', $headings, $bold );
  1566. $worksheet->write( 'A2', $data );
  1567. ###############################################################################
  1568. #
  1569. # Example 1. A minimal chart.
  1570. #
  1571. my $chart1 = $workbook->add_chart( type => 'column' );
  1572. # Add values only. Use the default categories.
  1573. $chart1->add_series( values => '=Sheet1!$B$2:$B$7' );
  1574. ###############################################################################
  1575. #
  1576. # Example 2. A minimal chart with user specified categories (X axis)
  1577. # and a series name.
  1578. #
  1579. my $chart2 = $workbook->add_chart( type => 'column' );
  1580. # Configure the series.
  1581. $chart2->add_series(
  1582. categories => '=Sheet1!$A$2:$A$7',
  1583. values => '=Sheet1!$B$2:$B$7',
  1584. name => 'Test data series 1',
  1585. );
  1586. ###############################################################################
  1587. #
  1588. # Example 3. Same as previous chart but with added title and axes labels.
  1589. #
  1590. my $chart3 = $workbook->add_chart( type => 'column' );
  1591. # Configure the series.
  1592. $chart3->add_series(
  1593. categories => '=Sheet1!$A$2:$A$7',
  1594. values => '=Sheet1!$B$2:$B$7',
  1595. name => 'Test data series 1',
  1596. );
  1597. # Add some labels.
  1598. $chart3->set_title( name => 'Results of sample analysis' );
  1599. $chart3->set_x_axis( name => 'Sample number' );
  1600. $chart3->set_y_axis( name => 'Sample length (cm)' );
  1601. ###############################################################################
  1602. #
  1603. # Example 4. Same as previous chart but with an added series and with a
  1604. # user specified chart sheet name.
  1605. #
  1606. my $chart4 = $workbook->add_chart( name => 'Results Chart', type => 'column' );
  1607. # Configure the series.
  1608. $chart4->add_series(
  1609. categories => '=Sheet1!$A$2:$A$7',
  1610. values => '=Sheet1!$B$2:$B$7',
  1611. name => 'Test data series 1',
  1612. );
  1613. # Add another series.
  1614. $chart4->add_series(
  1615. categories => '=Sheet1!$A$2:$A$7',
  1616. values => '=Sheet1!$C$2:$C$7',
  1617. name => 'Test data series 2',
  1618. );
  1619. # Add some labels.
  1620. $chart4->set_title( name => 'Results of sample analysis' );
  1621. $chart4->set_x_axis( name => 'Sample number' );
  1622. $chart4->set_y_axis( name => 'Sample length (cm)' );
  1623. ###############################################################################
  1624. #
  1625. # Example 5. Same as Example 3 but as an embedded chart.
  1626. #
  1627. my $chart5 = $workbook->add_chart( type => 'column', embedded => 1 );
  1628. # Configure the series.
  1629. $chart5->add_series(
  1630. categories => '=Sheet1!$A$2:$A$7',
  1631. values => '=Sheet1!$B$2:$B$7',
  1632. name => 'Test data series 1',
  1633. );
  1634. # Add some labels.
  1635. $chart5->set_title( name => 'Results of sample analysis' );
  1636. $chart5->set_x_axis( name => 'Sample number' );
  1637. $chart5->set_y_axis( name => 'Sample length (cm)' );
  1638. # Insert the chart into the main worksheet.
  1639. $worksheet->insert_chart( 'E2', $chart5 );
  1640. __END__
  1641. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/chart_column.pl>
  1642. =head2 Example: chart_line.pl
  1643. A simple demo of Line charts in Spreadsheet::WriteExcel.
  1644. =begin html
  1645. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/chart_line.jpg" width="640" height="420" alt="Output from chart_line.pl" /></center></p>
  1646. =end html
  1647. Source code for this example:
  1648. #!/usr/bin/perl -w
  1649. ###############################################################################
  1650. #
  1651. # A simple demo of Line charts in Spreadsheet::WriteExcel.
  1652. #
  1653. # reverse('©'), December 2009, John McNamara, jmcnamara@cpan.org
  1654. #
  1655. use strict;
  1656. use Spreadsheet::WriteExcel;
  1657. my $workbook = Spreadsheet::WriteExcel->new( 'chart_line.xls' );
  1658. my $worksheet = $workbook->add_worksheet();
  1659. my $bold = $workbook->add_format( bold => 1 );
  1660. # Add the worksheet data that the charts will refer to.
  1661. my $headings = [ 'Category', 'Values 1', 'Values 2' ];
  1662. my $data = [
  1663. [ 2, 3, 4, 5, 6, 7 ],
  1664. [ 1, 4, 5, 2, 1, 5 ],
  1665. [ 3, 6, 7, 5, 4, 3 ],
  1666. ];
  1667. $worksheet->write( 'A1', $headings, $bold );
  1668. $worksheet->write( 'A2', $data );
  1669. ###############################################################################
  1670. #
  1671. # Example 1. A minimal chart.
  1672. #
  1673. my $chart1 = $workbook->add_chart( type => 'line' );
  1674. # Add values only. Use the default categories.
  1675. $chart1->add_series( values => '=Sheet1!$B$2:$B$7' );
  1676. ###############################################################################
  1677. #
  1678. # Example 2. A minimal chart with user specified categories (X axis)
  1679. # and a series name.
  1680. #
  1681. my $chart2 = $workbook->add_chart( type => 'line' );
  1682. # Configure the series.
  1683. $chart2->add_series(
  1684. categories => '=Sheet1!$A$2:$A$7',
  1685. values => '=Sheet1!$B$2:$B$7',
  1686. name => 'Test data series 1',
  1687. );
  1688. ###############################################################################
  1689. #
  1690. # Example 3. Same as previous chart but with added title and axes labels.
  1691. #
  1692. my $chart3 = $workbook->add_chart( type => 'line' );
  1693. # Configure the series.
  1694. $chart3->add_series(
  1695. categories => '=Sheet1!$A$2:$A$7',
  1696. values => '=Sheet1!$B$2:$B$7',
  1697. name => 'Test data series 1',
  1698. );
  1699. # Add some labels.
  1700. $chart3->set_title( name => 'Results of sample analysis' );
  1701. $chart3->set_x_axis( name => 'Sample number' );
  1702. $chart3->set_y_axis( name => 'Sample length (cm)' );
  1703. ###############################################################################
  1704. #
  1705. # Example 4. Same as previous chart but with an added series and with a
  1706. # user specified chart sheet name.
  1707. #
  1708. my $chart4 = $workbook->add_chart( name => 'Results Chart', type => 'line' );
  1709. # Configure the series.
  1710. $chart4->add_series(
  1711. categories => '=Sheet1!$A$2:$A$7',
  1712. values => '=Sheet1!$B$2:$B$7',
  1713. name => 'Test data series 1',
  1714. );
  1715. # Add another series.
  1716. $chart4->add_series(
  1717. categories => '=Sheet1!$A$2:$A$7',
  1718. values => '=Sheet1!$C$2:$C$7',
  1719. name => 'Test data series 2',
  1720. );
  1721. # Add some labels.
  1722. $chart4->set_title( name => 'Results of sample analysis' );
  1723. $chart4->set_x_axis( name => 'Sample number' );
  1724. $chart4->set_y_axis( name => 'Sample length (cm)' );
  1725. ###############################################################################
  1726. #
  1727. # Example 5. Same as Example 3 but as an embedded chart.
  1728. #
  1729. my $chart5 = $workbook->add_chart( type => 'line', embedded => 1 );
  1730. # Configure the series.
  1731. $chart5->add_series(
  1732. categories => '=Sheet1!$A$2:$A$7',
  1733. values => '=Sheet1!$B$2:$B$7',
  1734. name => 'Test data series 1',
  1735. );
  1736. # Add some labels.
  1737. $chart5->set_title( name => 'Results of sample analysis' );
  1738. $chart5->set_x_axis( name => 'Sample number' );
  1739. $chart5->set_y_axis( name => 'Sample length (cm)' );
  1740. # Insert the chart into the main worksheet.
  1741. $worksheet->insert_chart( 'E2', $chart5 );
  1742. __END__
  1743. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/chart_line.pl>
  1744. =head2 Example: chart_pie.pl
  1745. A simple demo of Pie charts in Spreadsheet::WriteExcel.
  1746. =begin html
  1747. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/chart_pie.jpg" width="640" height="420" alt="Output from chart_pie.pl" /></center></p>
  1748. =end html
  1749. Source code for this example:
  1750. #!/usr/bin/perl -w
  1751. ###############################################################################
  1752. #
  1753. # A simple demo of Pie charts in Spreadsheet::WriteExcel.
  1754. #
  1755. # reverse('©'), December 2009, John McNamara, jmcnamara@cpan.org
  1756. #
  1757. use strict;
  1758. use Spreadsheet::WriteExcel;
  1759. my $workbook = Spreadsheet::WriteExcel->new( 'chart_pie.xls' );
  1760. my $worksheet = $workbook->add_worksheet();
  1761. my $bold = $workbook->add_format( bold => 1 );
  1762. # Add the worksheet data that the charts will refer to.
  1763. my $headings = [ 'Category', 'Values' ];
  1764. my $data = [
  1765. [ 'Apple', 'Cherry', 'Pecan' ],
  1766. [ 60, 30, 10 ],
  1767. ];
  1768. $worksheet->write( 'A1', $headings, $bold );
  1769. $worksheet->write( 'A2', $data );
  1770. ###############################################################################
  1771. #
  1772. # Example 1. A minimal chart.
  1773. #
  1774. my $chart1 = $workbook->add_chart( type => 'pie' );
  1775. # Add values only. Use the default categories.
  1776. $chart1->add_series( values => '=Sheet1!$B$2:$B$4' );
  1777. ###############################################################################
  1778. #
  1779. # Example 2. A minimal chart with user specified categories and a series name.
  1780. #
  1781. my $chart2 = $workbook->add_chart( type => 'pie' );
  1782. # Configure the series.
  1783. $chart2->add_series(
  1784. categories => '=Sheet1!$A$2:$A$4',
  1785. values => '=Sheet1!$B$2:$B$4',
  1786. name => 'Pie sales data',
  1787. );
  1788. ###############################################################################
  1789. #
  1790. # Example 3. Same as previous chart but with an added title.
  1791. #
  1792. my $chart3 = $workbook->add_chart( type => 'pie' );
  1793. # Configure the series.
  1794. $chart3->add_series(
  1795. categories => '=Sheet1!$A$2:$A$4',
  1796. values => '=Sheet1!$B$2:$B$4',
  1797. name => 'Pie sales data',
  1798. );
  1799. # Add a title.
  1800. $chart3->set_title( name => 'Popular Pie Types' );
  1801. ###############################################################################
  1802. #
  1803. # Example 4. Same as previous chart with a user specified chart sheet name.
  1804. #
  1805. my $chart4 = $workbook->add_chart( name => 'Results Chart', type => 'pie' );
  1806. # Configure the series.
  1807. $chart4->add_series(
  1808. categories => '=Sheet1!$A$2:$A$4',
  1809. values => '=Sheet1!$B$2:$B$4',
  1810. name => 'Pie sales data',
  1811. );
  1812. # The other chart_*.pl examples add a second series in example 4 but additional
  1813. # series aren't plotted in a pie chart.
  1814. # Add a title.
  1815. $chart4->set_title( name => 'Popular Pie Types' );
  1816. ###############################################################################
  1817. #
  1818. # Example 5. Same as Example 3 but as an embedded chart.
  1819. #
  1820. my $chart5 = $workbook->add_chart( type => 'pie', embedded => 1 );
  1821. # Configure the series.
  1822. $chart5->add_series(
  1823. categories => '=Sheet1!$A$2:$A$4',
  1824. values => '=Sheet1!$B$2:$B$4',
  1825. name => 'Pie sales data',
  1826. );
  1827. # Add a title.
  1828. $chart5->set_title( name => 'Popular Pie Types' );
  1829. # Insert the chart into the main worksheet.
  1830. $worksheet->insert_chart( 'D2', $chart5 );
  1831. __END__
  1832. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/chart_pie.pl>
  1833. =head2 Example: chart_scatter.pl
  1834. A simple demo of Scatter charts in Spreadsheet::WriteExcel.
  1835. =begin html
  1836. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/chart_scatter.jpg" width="640" height="420" alt="Output from chart_scatter.pl" /></center></p>
  1837. =end html
  1838. Source code for this example:
  1839. #!/usr/bin/perl -w
  1840. ###############################################################################
  1841. #
  1842. # A simple demo of Scatter charts in Spreadsheet::WriteExcel.
  1843. #
  1844. # reverse('©'), December 2009, John McNamara, jmcnamara@cpan.org
  1845. #
  1846. use strict;
  1847. use Spreadsheet::WriteExcel;
  1848. my $workbook = Spreadsheet::WriteExcel->new( 'chart_scatter.xls' );
  1849. my $worksheet = $workbook->add_worksheet();
  1850. my $bold = $workbook->add_format( bold => 1 );
  1851. # Add the worksheet data that the charts will refer to.
  1852. my $headings = [ 'Category', 'Values 1', 'Values 2' ];
  1853. my $data = [
  1854. [ 2, 3, 4, 5, 6, 7 ],
  1855. [ 1, 4, 5, 2, 1, 5 ],
  1856. [ 3, 6, 7, 5, 4, 3 ],
  1857. ];
  1858. $worksheet->write( 'A1', $headings, $bold );
  1859. $worksheet->write( 'A2', $data );
  1860. ###############################################################################
  1861. #
  1862. # Example 1. A minimal chart.
  1863. #
  1864. my $chart1 = $workbook->add_chart( type => 'scatter' );
  1865. # Add values only. Use the default categories.
  1866. $chart1->add_series( values => '=Sheet1!$B$2:$B$7' );
  1867. ###############################################################################
  1868. #
  1869. # Example 2. A minimal chart with user specified categories (X axis)
  1870. # and a series name.
  1871. #
  1872. my $chart2 = $workbook->add_chart( type => 'scatter' );
  1873. # Configure the series.
  1874. $chart2->add_series(
  1875. categories => '=Sheet1!$A$2:$A$7',
  1876. values => '=Sheet1!$B$2:$B$7',
  1877. name => 'Test data series 1',
  1878. );
  1879. ###############################################################################
  1880. #
  1881. # Example 3. Same as previous chart but with added title and axes labels.
  1882. #
  1883. my $chart3 = $workbook->add_chart( type => 'scatter' );
  1884. # Configure the series.
  1885. $chart3->add_series(
  1886. categories => '=Sheet1!$A$2:$A$7',
  1887. values => '=Sheet1!$B$2:$B$7',
  1888. name => 'Test data series 1',
  1889. );
  1890. # Add some labels.
  1891. $chart3->set_title( name => 'Results of sample analysis' );
  1892. $chart3->set_x_axis( name => 'Sample number' );
  1893. $chart3->set_y_axis( name => 'Sample length (cm)' );
  1894. ###############################################################################
  1895. #
  1896. # Example 4. Same as previous chart but with an added series and with a
  1897. # user specified chart sheet name.
  1898. #
  1899. my $chart4 = $workbook->add_chart( name => 'Results Chart', type => 'scatter' );
  1900. # Configure the series.
  1901. $chart4->add_series(
  1902. categories => '=Sheet1!$A$2:$A$7',
  1903. values => '=Sheet1!$B$2:$B$7',
  1904. name => 'Test data series 1',
  1905. );
  1906. # Add another series.
  1907. $chart4->add_series(
  1908. categories => '=Sheet1!$A$2:$A$7',
  1909. values => '=Sheet1!$C$2:$C$7',
  1910. name => 'Test data series 2',
  1911. );
  1912. # Add some labels.
  1913. $chart4->set_title( name => 'Results of sample analysis' );
  1914. $chart4->set_x_axis( name => 'Sample number' );
  1915. $chart4->set_y_axis( name => 'Sample length (cm)' );
  1916. ###############################################################################
  1917. #
  1918. # Example 5. Same as Example 3 but as an embedded chart.
  1919. #
  1920. my $chart5 = $workbook->add_chart( type => 'scatter', embedded => 1 );
  1921. # Configure the series.
  1922. $chart5->add_series(
  1923. categories => '=Sheet1!$A$2:$A$7',
  1924. values => '=Sheet1!$B$2:$B$7',
  1925. name => 'Test data series 1',
  1926. );
  1927. # Add some labels.
  1928. $chart5->set_title( name => 'Results of sample analysis' );
  1929. $chart5->set_x_axis( name => 'Sample number' );
  1930. $chart5->set_y_axis( name => 'Sample length (cm)' );
  1931. # Insert the chart into the main worksheet.
  1932. $worksheet->insert_chart( 'E2', $chart5 );
  1933. __END__
  1934. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/chart_scatter.pl>
  1935. =head2 Example: chart_stock.pl
  1936. A simple demo of Stock charts in Spreadsheet::WriteExcel.
  1937. =begin html
  1938. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/chart_stock.jpg" width="640" height="420" alt="Output from chart_stock.pl" /></center></p>
  1939. =end html
  1940. Source code for this example:
  1941. #!/usr/bin/perl -w
  1942. ###############################################################################
  1943. #
  1944. # A simple demo of Stock charts in Spreadsheet::WriteExcel.
  1945. #
  1946. # reverse('©'), January 2010, John McNamara, jmcnamara@cpan.org
  1947. #
  1948. use strict;
  1949. use Spreadsheet::WriteExcel;
  1950. my $workbook = Spreadsheet::WriteExcel->new( 'chart_stock.xls' );
  1951. my $worksheet = $workbook->add_worksheet();
  1952. ###############################################################################
  1953. #
  1954. # Set up the data worksheet that the charts will refer to. We read the example
  1955. # data from the __DATA__ section at the end of the file. This simulates
  1956. # reading the data from a database or other source.
  1957. #
  1958. # The default Excel Stock chart is an Open-High-Low-Close chart. Therefore
  1959. # we will need data for each of those series.
  1960. #
  1961. # The layout of the __DATA__ section is similar to the layout of the worksheet.
  1962. #
  1963. # Add some formats.
  1964. my $bold = $workbook->add_format( bold => 1 );
  1965. my $date_format = $workbook->add_format( num_format => 'dd/mm/yyyy' );
  1966. # Increase the width of the column used for date to make it clearer.
  1967. $worksheet->set_column( 'A:A', 12 );
  1968. # Read the data from the __DATA__ section at the end. In a real example this
  1969. # would probably be a database query.
  1970. my @stock_data;
  1971. while ( <DATA> ) {
  1972. next unless /\S/; # Skip blank lines.
  1973. next if /^#/; # Skip comments.
  1974. push @stock_data, [split];
  1975. }
  1976. # Write the data to the worksheet.
  1977. my $row = 0;
  1978. my $col = 0;
  1979. my $headers = shift @stock_data;
  1980. $worksheet->write( $row++, $col, $headers, $bold );
  1981. for my $stock_data ( @stock_data ) {
  1982. my @data = @$stock_data;
  1983. my $date = shift @data;
  1984. $worksheet->write( $row, $col, $date, $date_format );
  1985. $worksheet->write( $row, $col + 1, \@data );
  1986. $row++;
  1987. }
  1988. ###############################################################################
  1989. #
  1990. # Example 1. A default Open-High-Low-Close chart with series names, axes labels
  1991. # and a title.
  1992. #
  1993. my $chart1 = $workbook->add_chart( type => 'stock' );
  1994. # Add a series for each of the Open-High-Low-Close columns. The categories are
  1995. # the dates in the first column.
  1996. $chart1->add_series(
  1997. categories => '=Sheet1!$A$2:$A$10',
  1998. values => '=Sheet1!$B$2:$B$10',
  1999. name => 'Open',
  2000. );
  2001. $chart1->add_series(
  2002. categories => '=Sheet1!$A$2:$A$10',
  2003. values => '=Sheet1!$C$2:$C$10',
  2004. name => 'High',
  2005. );
  2006. $chart1->add_series(
  2007. categories => '=Sheet1!$A$2:$A$10',
  2008. values => '=Sheet1!$D$2:$D$10',
  2009. name => 'Low',
  2010. );
  2011. $chart1->add_series(
  2012. categories => '=Sheet1!$A$2:$A$10',
  2013. values => '=Sheet1!$E$2:$E$10',
  2014. name => 'Close',
  2015. );
  2016. # Add a chart title and axes labels.
  2017. $chart1->set_title( name => 'Open-High-Low-Close', );
  2018. $chart1->set_x_axis( name => 'Date', );
  2019. $chart1->set_y_axis( name => 'Share price', );
  2020. ###############################################################################
  2021. #
  2022. # Example 2. Same as the previous as an embedded chart.
  2023. #
  2024. my $chart2 = $workbook->add_chart( type => 'stock', embedded => 1 );
  2025. # Add a series for each of the Open-High-Low-Close columns. The categories are
  2026. # the dates in the first column.
  2027. $chart2->add_series(
  2028. categories => '=Sheet1!$A$2:$A$10',
  2029. values => '=Sheet1!$B$2:$B$10',
  2030. name => 'Open',
  2031. );
  2032. $chart2->add_series(
  2033. categories => '=Sheet1!$A$2:$A$10',
  2034. values => '=Sheet1!$C$2:$C$10',
  2035. name => 'High',
  2036. );
  2037. $chart2->add_series(
  2038. categories => '=Sheet1!$A$2:$A$10',
  2039. values => '=Sheet1!$D$2:$D$10',
  2040. name => 'Low',
  2041. );
  2042. $chart2->add_series(
  2043. categories => '=Sheet1!$A$2:$A$10',
  2044. values => '=Sheet1!$E$2:$E$10',
  2045. name => 'Close',
  2046. );
  2047. # Add a chart title and axes labels.
  2048. $chart2->set_title( name => 'Open-High-Low-Close', );
  2049. $chart2->set_x_axis( name => 'Date', );
  2050. $chart2->set_y_axis( name => 'Share price', );
  2051. # Insert the chart into the main worksheet.
  2052. $worksheet->insert_chart( 'G2', $chart2 );
  2053. __DATA__
  2054. # Some sample stock data used for charting.
  2055. Date Open High Low Close
  2056. 2009-08-19 100.00 104.06 95.96 100.34
  2057. 2009-08-20 101.01 109.08 100.50 108.31
  2058. 2009-08-23 110.75 113.48 109.05 109.40
  2059. 2009-08-24 111.24 111.60 103.57 104.87
  2060. 2009-08-25 104.96 108.00 103.88 106.00
  2061. 2009-08-26 104.95 107.95 104.66 107.91
  2062. 2009-08-27 108.10 108.62 105.69 106.15
  2063. 2009-08-30 105.28 105.49 102.01 102.01
  2064. 2009-08-31 102.30 103.71 102.16 102.37
  2065. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/chart_stock.pl>
  2066. =head2 Example: chess.pl
  2067. Example of formatting using the Spreadsheet::WriteExcel module via
  2068. property hashes.
  2069. Setting format properties via hashes of values is useful when you have
  2070. to deal with a large number of similar formats. Consider for example a
  2071. chess board pattern with black squares, white unformatted squares and
  2072. a border.
  2073. This relatively simple example requires 14 separate Format
  2074. objects although there are only 5 different properties: black
  2075. background, top border, bottom border, left border and right border.
  2076. Using property hashes it is possible to define these 5 sets of
  2077. properties and then add them together to create the 14 Format
  2078. configurations.
  2079. =begin html
  2080. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/chess.jpg" width="640" height="420" alt="Output from chess.pl" /></center></p>
  2081. =end html
  2082. Source code for this example:
  2083. #!/usr/bin/perl -w
  2084. ########################################################################
  2085. #
  2086. # Example of formatting using the Spreadsheet::WriteExcel module via
  2087. # property hashes.
  2088. #
  2089. # Setting format properties via hashes of values is useful when you have
  2090. # to deal with a large number of similar formats. Consider for example a
  2091. # chess board pattern with black squares, white unformatted squares and
  2092. # a border.
  2093. #
  2094. # This relatively simple example requires 14 separate Format
  2095. # objects although there are only 5 different properties: black
  2096. # background, top border, bottom border, left border and right border.
  2097. #
  2098. # Using property hashes it is possible to define these 5 sets of
  2099. # properties and then add them together to create the 14 Format
  2100. # configurations.
  2101. #
  2102. # reverse('©'), July 2001, John McNamara, jmcnamara@cpan.org
  2103. #
  2104. use strict;
  2105. use Spreadsheet::WriteExcel;
  2106. my $workbook = Spreadsheet::WriteExcel->new("chess.xls");
  2107. my $worksheet = $workbook->add_worksheet();
  2108. # Some row and column formatting
  2109. $worksheet->set_column('B:I', 10);
  2110. for my $i (1..8) {
  2111. $worksheet->set_row($i, 50);
  2112. }
  2113. # Define the property hashes
  2114. #
  2115. my %black = (
  2116. 'fg_color' => 'black',
  2117. 'pattern' => 1,
  2118. );
  2119. my %top = ( 'top' => 6 );
  2120. my %bottom = ( 'bottom' => 6 );
  2121. my %left = ( 'left' => 6 );
  2122. my %right = ( 'right' => 6 );
  2123. # Define the formats
  2124. #
  2125. my $format01 = $workbook->add_format(%top, %left );
  2126. my $format02 = $workbook->add_format(%top, %black );
  2127. my $format03 = $workbook->add_format(%top, );
  2128. my $format04 = $workbook->add_format(%top, %right, %black );
  2129. my $format05 = $workbook->add_format(%left );
  2130. my $format06 = $workbook->add_format(%black );
  2131. my $format07 = $workbook->add_format( );
  2132. my $format08 = $workbook->add_format(%right, %black );
  2133. my $format09 = $workbook->add_format(%right );
  2134. my $format10 = $workbook->add_format(%left, %black );
  2135. my $format11 = $workbook->add_format(%bottom, %left, %black );
  2136. my $format12 = $workbook->add_format(%bottom );
  2137. my $format13 = $workbook->add_format(%bottom, %black );
  2138. my $format14 = $workbook->add_format(%bottom, %right );
  2139. # Draw the pattern
  2140. $worksheet->write('B2', '', $format01);
  2141. $worksheet->write('C2', '', $format02);
  2142. $worksheet->write('D2', '', $format03);
  2143. $worksheet->write('E2', '', $format02);
  2144. $worksheet->write('F2', '', $format03);
  2145. $worksheet->write('G2', '', $format02);
  2146. $worksheet->write('H2', '', $format03);
  2147. $worksheet->write('I2', '', $format04);
  2148. $worksheet->write('B3', '', $format10);
  2149. $worksheet->write('C3', '', $format07);
  2150. $worksheet->write('D3', '', $format06);
  2151. $worksheet->write('E3', '', $format07);
  2152. $worksheet->write('F3', '', $format06);
  2153. $worksheet->write('G3', '', $format07);
  2154. $worksheet->write('H3', '', $format06);
  2155. $worksheet->write('I3', '', $format09);
  2156. $worksheet->write('B4', '', $format05);
  2157. $worksheet->write('C4', '', $format06);
  2158. $worksheet->write('D4', '', $format07);
  2159. $worksheet->write('E4', '', $format06);
  2160. $worksheet->write('F4', '', $format07);
  2161. $worksheet->write('G4', '', $format06);
  2162. $worksheet->write('H4', '', $format07);
  2163. $worksheet->write('I4', '', $format08);
  2164. $worksheet->write('B5', '', $format10);
  2165. $worksheet->write('C5', '', $format07);
  2166. $worksheet->write('D5', '', $format06);
  2167. $worksheet->write('E5', '', $format07);
  2168. $worksheet->write('F5', '', $format06);
  2169. $worksheet->write('G5', '', $format07);
  2170. $worksheet->write('H5', '', $format06);
  2171. $worksheet->write('I5', '', $format09);
  2172. $worksheet->write('B6', '', $format05);
  2173. $worksheet->write('C6', '', $format06);
  2174. $worksheet->write('D6', '', $format07);
  2175. $worksheet->write('E6', '', $format06);
  2176. $worksheet->write('F6', '', $format07);
  2177. $worksheet->write('G6', '', $format06);
  2178. $worksheet->write('H6', '', $format07);
  2179. $worksheet->write('I6', '', $format08);
  2180. $worksheet->write('B7', '', $format10);
  2181. $worksheet->write('C7', '', $format07);
  2182. $worksheet->write('D7', '', $format06);
  2183. $worksheet->write('E7', '', $format07);
  2184. $worksheet->write('F7', '', $format06);
  2185. $worksheet->write('G7', '', $format07);
  2186. $worksheet->write('H7', '', $format06);
  2187. $worksheet->write('I7', '', $format09);
  2188. $worksheet->write('B8', '', $format05);
  2189. $worksheet->write('C8', '', $format06);
  2190. $worksheet->write('D8', '', $format07);
  2191. $worksheet->write('E8', '', $format06);
  2192. $worksheet->write('F8', '', $format07);
  2193. $worksheet->write('G8', '', $format06);
  2194. $worksheet->write('H8', '', $format07);
  2195. $worksheet->write('I8', '', $format08);
  2196. $worksheet->write('B9', '', $format11);
  2197. $worksheet->write('C9', '', $format12);
  2198. $worksheet->write('D9', '', $format13);
  2199. $worksheet->write('E9', '', $format12);
  2200. $worksheet->write('F9', '', $format13);
  2201. $worksheet->write('G9', '', $format12);
  2202. $worksheet->write('H9', '', $format13);
  2203. $worksheet->write('I9', '', $format14);
  2204. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/chess.pl>
  2205. =head2 Example: colors.pl
  2206. Demonstrates Spreadsheet::WriteExcel's named colors and the Excel color
  2207. palette.
  2208. The set_custom_color() Worksheet method can be used to override one of the
  2209. built-in palette values with a more suitable colour. See the main docs.
  2210. =begin html
  2211. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/colors.jpg" width="640" height="420" alt="Output from colors.pl" /></center></p>
  2212. =end html
  2213. Source code for this example:
  2214. #!/usr/bin/perl -w
  2215. ################################################################################
  2216. #
  2217. # Demonstrates Spreadsheet::WriteExcel's named colors and the Excel color
  2218. # palette.
  2219. #
  2220. # The set_custom_color() Worksheet method can be used to override one of the
  2221. # built-in palette values with a more suitable colour. See the main docs.
  2222. #
  2223. # reverse('©'), March 2002, John McNamara, jmcnamara@cpan.org
  2224. #
  2225. use strict;
  2226. use Spreadsheet::WriteExcel;
  2227. my $workbook = Spreadsheet::WriteExcel->new("colors.xls");
  2228. # Some common formats
  2229. my $center = $workbook->add_format(align => 'center');
  2230. my $heading = $workbook->add_format(align => 'center', bold => 1);
  2231. ######################################################################
  2232. #
  2233. # Demonstrate the named colors.
  2234. #
  2235. my %colors = (
  2236. 0x08, 'black',
  2237. 0x0C, 'blue',
  2238. 0x10, 'brown',
  2239. 0x0F, 'cyan',
  2240. 0x17, 'gray',
  2241. 0x11, 'green',
  2242. 0x0B, 'lime',
  2243. 0x0E, 'magenta',
  2244. 0x12, 'navy',
  2245. 0x35, 'orange',
  2246. 0x21, 'pink',
  2247. 0x14, 'purple',
  2248. 0x0A, 'red',
  2249. 0x16, 'silver',
  2250. 0x09, 'white',
  2251. 0x0D, 'yellow',
  2252. );
  2253. my $worksheet1 = $workbook->add_worksheet('Named colors');
  2254. $worksheet1->set_column(0, 3, 15);
  2255. $worksheet1->write(0, 0, "Index", $heading);
  2256. $worksheet1->write(0, 1, "Index", $heading);
  2257. $worksheet1->write(0, 2, "Name", $heading);
  2258. $worksheet1->write(0, 3, "Color", $heading);
  2259. my $i = 1;
  2260. while (my($index, $color) = each %colors) {
  2261. my $format = $workbook->add_format(
  2262. fg_color => $color,
  2263. pattern => 1,
  2264. border => 1
  2265. );
  2266. $worksheet1->write($i+1, 0, $index, $center);
  2267. $worksheet1->write($i+1, 1, sprintf("0x%02X", $index), $center);
  2268. $worksheet1->write($i+1, 2, $color, $center);
  2269. $worksheet1->write($i+1, 3, '', $format);
  2270. $i++;
  2271. }
  2272. ######################################################################
  2273. #
  2274. # Demonstrate the standard Excel colors in the range 8..63.
  2275. #
  2276. my $worksheet2 = $workbook->add_worksheet('Standard colors');
  2277. $worksheet2->set_column(0, 3, 15);
  2278. $worksheet2->write(0, 0, "Index", $heading);
  2279. $worksheet2->write(0, 1, "Index", $heading);
  2280. $worksheet2->write(0, 2, "Color", $heading);
  2281. $worksheet2->write(0, 3, "Name", $heading);
  2282. for my $i (8..63) {
  2283. my $format = $workbook->add_format(
  2284. fg_color => $i,
  2285. pattern => 1,
  2286. border => 1
  2287. );
  2288. $worksheet2->write(($i -7), 0, $i, $center);
  2289. $worksheet2->write(($i -7), 1, sprintf("0x%02X", $i), $center);
  2290. $worksheet2->write(($i -7), 2, '', $format);
  2291. # Add the color names
  2292. if (exists $colors{$i}) {
  2293. $worksheet2->write(($i -7), 3, $colors{$i}, $center);
  2294. }
  2295. }
  2296. __END__
  2297. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/colors.pl>
  2298. =head2 Example: comments1.pl
  2299. This example demonstrates writing cell comments.
  2300. A cell comment is indicated in Excel by a small red triangle in the upper
  2301. right-hand corner of the cell.
  2302. For more advanced comment options see comments2.pl.
  2303. =begin html
  2304. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/comments1.jpg" width="640" height="420" alt="Output from comments1.pl" /></center></p>
  2305. =end html
  2306. Source code for this example:
  2307. #!/usr/bin/perl -w
  2308. ###############################################################################
  2309. #
  2310. # This example demonstrates writing cell comments.
  2311. #
  2312. # A cell comment is indicated in Excel by a small red triangle in the upper
  2313. # right-hand corner of the cell.
  2314. #
  2315. # For more advanced comment options see comments2.pl.
  2316. #
  2317. # reverse('©'), November 2005, John McNamara, jmcnamara@cpan.org
  2318. #
  2319. use strict;
  2320. use Spreadsheet::WriteExcel;
  2321. my $workbook = Spreadsheet::WriteExcel->new("comments1.xls");
  2322. my $worksheet = $workbook->add_worksheet();
  2323. $worksheet->write ('A1', 'Hello' );
  2324. $worksheet->write_comment('A1', 'This is a comment');
  2325. __END__
  2326. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/comments1.pl>
  2327. =head2 Example: comments2.pl
  2328. This example demonstrates writing cell comments.
  2329. A cell comment is indicated in Excel by a small red triangle in the upper
  2330. right-hand corner of the cell.
  2331. Each of the worksheets demonstrates different features of cell comments.
  2332. =begin html
  2333. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/comments2.jpg" width="640" height="420" alt="Output from comments2.pl" /></center></p>
  2334. =end html
  2335. Source code for this example:
  2336. #!/usr/bin/perl -w
  2337. ###############################################################################
  2338. #
  2339. # This example demonstrates writing cell comments.
  2340. #
  2341. # A cell comment is indicated in Excel by a small red triangle in the upper
  2342. # right-hand corner of the cell.
  2343. #
  2344. # Each of the worksheets demonstrates different features of cell comments.
  2345. #
  2346. # reverse('©'), November 2005, John McNamara, jmcnamara@cpan.org
  2347. #
  2348. use strict;
  2349. use Spreadsheet::WriteExcel;
  2350. my $workbook = Spreadsheet::WriteExcel->new("comments2.xls");
  2351. my $text_wrap = $workbook->add_format(text_wrap => 1, valign => 'top');
  2352. my $worksheet1 = $workbook->add_worksheet();
  2353. my $worksheet2 = $workbook->add_worksheet();
  2354. my $worksheet3 = $workbook->add_worksheet();
  2355. my $worksheet4 = $workbook->add_worksheet();
  2356. my $worksheet5 = $workbook->add_worksheet();
  2357. my $worksheet6 = $workbook->add_worksheet();
  2358. my $worksheet7 = $workbook->add_worksheet();
  2359. my $worksheet8 = $workbook->add_worksheet();
  2360. # Variables that we will use in each example.
  2361. my $cell_text = '';
  2362. my $comment = '';
  2363. ###############################################################################
  2364. #
  2365. # Example 1. Demonstrates a simple cell comment without formatting and Unicode
  2366. # comments encoded as UTF-16 and as UTF-8.
  2367. #
  2368. # Set up some formatting.
  2369. $worksheet1->set_column('C:C', 25);
  2370. $worksheet1->set_row(2, 50);
  2371. $worksheet1->set_row(5, 50);
  2372. # Simple ascii string.
  2373. $cell_text = 'Hold the mouse over this cell to see the comment.';
  2374. $comment = 'This is a comment.';
  2375. $worksheet1->write ('C3', $cell_text, $text_wrap);
  2376. $worksheet1->write_comment('C3', $comment);
  2377. # UTF-16 string.
  2378. $cell_text = 'This is a UTF-16 comment.';
  2379. $comment = pack "n", 0x263a;
  2380. $worksheet1->write ('C6', $cell_text, $text_wrap);
  2381. $worksheet1->write_comment('C6', $comment, encoding => 1);
  2382. # UTF-8 string in perl 5.8.
  2383. if ($] >= 5.008) {
  2384. $worksheet1->set_row(8, 50);
  2385. $cell_text = 'This is a UTF-8 string.';
  2386. $comment = chr 0x263a;
  2387. $worksheet1->write ('C9', $cell_text, $text_wrap);
  2388. $worksheet1->write_comment('C9', $comment);
  2389. }
  2390. ###############################################################################
  2391. #
  2392. # Example 2. Demonstrates visible and hidden comments.
  2393. #
  2394. # Set up some formatting.
  2395. $worksheet2->set_column('C:C', 25);
  2396. $worksheet2->set_row(2, 50);
  2397. $worksheet2->set_row(5, 50);
  2398. $cell_text = 'This cell comment is visible.';
  2399. $comment = 'Hello.';
  2400. $worksheet2->write ('C3', $cell_text, $text_wrap);
  2401. $worksheet2->write_comment('C3', $comment, visible => 1);
  2402. $cell_text = "This cell comment isn't visible (the default).";
  2403. $comment = 'Hello.';
  2404. $worksheet2->write ('C6', $cell_text, $text_wrap);
  2405. $worksheet2->write_comment('C6', $comment);
  2406. ###############################################################################
  2407. #
  2408. # Example 3. Demonstrates visible and hidden comments set at the worksheet
  2409. # level.
  2410. #
  2411. # Set up some formatting.
  2412. $worksheet3->set_column('C:C', 25);
  2413. $worksheet3->set_row(2, 50);
  2414. $worksheet3->set_row(5, 50);
  2415. $worksheet3->set_row(8, 50);
  2416. # Make all comments on the worksheet visible.
  2417. $worksheet3->show_comments();
  2418. $cell_text = 'This cell comment is visible, explicitly.';
  2419. $comment = 'Hello.';
  2420. $worksheet3->write ('C3', $cell_text, $text_wrap);
  2421. $worksheet3->write_comment('C3', $comment, visible => 1);
  2422. $cell_text = 'This cell comment is also visible because '.
  2423. 'we used show_comments().';
  2424. $comment = 'Hello.';
  2425. $worksheet3->write ('C6', $cell_text, $text_wrap);
  2426. $worksheet3->write_comment('C6', $comment);
  2427. $cell_text = 'However, we can still override it locally.';
  2428. $comment = 'Hello.';
  2429. $worksheet3->write ('C9', $cell_text, $text_wrap);
  2430. $worksheet3->write_comment('C9', $comment, visible => 0);
  2431. ###############################################################################
  2432. #
  2433. # Example 4. Demonstrates changes to the comment box dimensions.
  2434. #
  2435. # Set up some formatting.
  2436. $worksheet4->set_column('C:C', 25);
  2437. $worksheet4->set_row(2, 50);
  2438. $worksheet4->set_row(5, 50);
  2439. $worksheet4->set_row(8, 50);
  2440. $worksheet4->set_row(15, 50);
  2441. $worksheet4->show_comments();
  2442. $cell_text = 'This cell comment is default size.';
  2443. $comment = 'Hello.';
  2444. $worksheet4->write ('C3', $cell_text, $text_wrap);
  2445. $worksheet4->write_comment('C3', $comment);
  2446. $cell_text = 'This cell comment is twice as wide.';
  2447. $comment = 'Hello.';
  2448. $worksheet4->write ('C6', $cell_text, $text_wrap);
  2449. $worksheet4->write_comment('C6', $comment, x_scale => 2);
  2450. $cell_text = 'This cell comment is twice as high.';
  2451. $comment = 'Hello.';
  2452. $worksheet4->write ('C9', $cell_text, $text_wrap);
  2453. $worksheet4->write_comment('C9', $comment, y_scale => 2);
  2454. $cell_text = 'This cell comment is scaled in both directions.';
  2455. $comment = 'Hello.';
  2456. $worksheet4->write ('C16', $cell_text, $text_wrap);
  2457. $worksheet4->write_comment('C16', $comment, x_scale => 1.2, y_scale => 0.8);
  2458. $cell_text = 'This cell comment has width and height specified in pixels.';
  2459. $comment = 'Hello.';
  2460. $worksheet4->write ('C19', $cell_text, $text_wrap);
  2461. $worksheet4->write_comment('C19', $comment, width => 200, height => 20);
  2462. ###############################################################################
  2463. #
  2464. # Example 5. Demonstrates changes to the cell comment position.
  2465. #
  2466. $worksheet5->set_column('C:C', 25);
  2467. $worksheet5->set_row(2, 50);
  2468. $worksheet5->set_row(5, 50);
  2469. $worksheet5->set_row(8, 50);
  2470. $worksheet5->set_row(11, 50);
  2471. $worksheet5->show_comments();
  2472. $cell_text = 'This cell comment is in the default position.';
  2473. $comment = 'Hello.';
  2474. $worksheet5->write ('C3', $cell_text, $text_wrap);
  2475. $worksheet5->write_comment('C3', $comment);
  2476. $cell_text = 'This cell comment has been moved to another cell.';
  2477. $comment = 'Hello.';
  2478. $worksheet5->write ('C6', $cell_text, $text_wrap);
  2479. $worksheet5->write_comment('C6', $comment, start_cell => 'E4');
  2480. $cell_text = 'This cell comment has been moved to another cell.';
  2481. $comment = 'Hello.';
  2482. $worksheet5->write ('C9', $cell_text, $text_wrap);
  2483. $worksheet5->write_comment('C9', $comment, start_row => 8, start_col => 4);
  2484. $cell_text = 'This cell comment has been shifted within its default cell.';
  2485. $comment = 'Hello.';
  2486. $worksheet5->write ('C12', $cell_text, $text_wrap);
  2487. $worksheet5->write_comment('C12', $comment, x_offset => 30, y_offset => 12);
  2488. ###############################################################################
  2489. #
  2490. # Example 6. Demonstrates changes to the comment background colour.
  2491. #
  2492. $worksheet6->set_column('C:C', 25);
  2493. $worksheet6->set_row(2, 50);
  2494. $worksheet6->set_row(5, 50);
  2495. $worksheet6->set_row(8, 50);
  2496. $worksheet6->show_comments();
  2497. $cell_text = 'This cell comment has a different colour.';
  2498. $comment = 'Hello.';
  2499. $worksheet6->write ('C3', $cell_text, $text_wrap);
  2500. $worksheet6->write_comment('C3', $comment, color => 'green');
  2501. $cell_text = 'This cell comment has the default colour.';
  2502. $comment = 'Hello.';
  2503. $worksheet6->write ('C6', $cell_text, $text_wrap);
  2504. $worksheet6->write_comment('C6', $comment);
  2505. $cell_text = 'This cell comment has a different colour.';
  2506. $comment = 'Hello.';
  2507. $worksheet6->write ('C9', $cell_text, $text_wrap);
  2508. $worksheet6->write_comment('C9', $comment, color => 0x35);
  2509. ###############################################################################
  2510. #
  2511. # Example 7. Demonstrates how to set the cell comment author.
  2512. #
  2513. $worksheet7->set_column('C:C', 30);
  2514. $worksheet7->set_row(2, 50);
  2515. $worksheet7->set_row(5, 50);
  2516. $worksheet7->set_row(8, 50);
  2517. $worksheet7->set_row(11, 50);
  2518. my $author = '';
  2519. my $cell = 'C3';
  2520. $cell_text = "Move the mouse over this cell and you will see 'Cell commented ".
  2521. "by $author' (blank) in the status bar at the bottom";
  2522. $comment = 'Hello.';
  2523. $worksheet7->write ($cell, $cell_text, $text_wrap);
  2524. $worksheet7->write_comment($cell, $comment);
  2525. $author = 'Perl';
  2526. $cell = 'C6';
  2527. $cell_text = "Move the mouse over this cell and you will see 'Cell commented ".
  2528. "by $author' in the status bar at the bottom";
  2529. $comment = 'Hello.';
  2530. $worksheet7->write ($cell, $cell_text, $text_wrap);
  2531. $worksheet7->write_comment($cell, $comment, author => $author);
  2532. $author = pack "n", 0x20AC; # UTF-16 Euro
  2533. $cell = 'C9';
  2534. $cell_text = "Move the mouse over this cell and you will see 'Cell commented ".
  2535. "by Euro' in the status bar at the bottom";
  2536. $comment = 'Hello.';
  2537. $worksheet7->write ($cell, $cell_text, $text_wrap);
  2538. $worksheet7->write_comment($cell, $comment, author => $author,
  2539. author_encoding => 1 );
  2540. # UTF-8 string in perl 5.8.
  2541. if ($] >= 5.008) {
  2542. $author = chr 0x20AC;
  2543. $cell = 'C12';
  2544. $cell_text = "Move the mouse over this cell and you will see 'Cell commented ".
  2545. "by $author' in the status bar at the bottom";
  2546. $comment = 'Hello.';
  2547. $worksheet7->write ($cell, $cell_text, $text_wrap);
  2548. $worksheet7->write_comment($cell, $comment, author => $author);
  2549. }
  2550. ###############################################################################
  2551. #
  2552. # Example 8. Demonstrates the need to explicitly set the row height.
  2553. #
  2554. # Set up some formatting.
  2555. $worksheet8->set_column('C:C', 25);
  2556. $worksheet8->set_row(2, 80);
  2557. $worksheet8->show_comments();
  2558. $cell_text = 'The height of this row has been adjusted explicitly using ' .
  2559. 'set_row(). The size of the comment box is adjusted ' .
  2560. 'accordingly by WriteExcel.';
  2561. $comment = 'Hello.';
  2562. $worksheet8->write ('C3', $cell_text, $text_wrap);
  2563. $worksheet8->write_comment('C3', $comment);
  2564. $cell_text = 'The height of this row has been adjusted by Excel due to the ' .
  2565. 'text wrap property being set. Unfortunately this means that ' .
  2566. 'the height of the row is unknown to WriteExcel at run time ' .
  2567. "and thus the comment box is stretched as well.\n\n" .
  2568. 'Use set_row() to specify the row height explicitly to avoid ' .
  2569. 'this problem.';
  2570. $comment = 'Hello.';
  2571. $worksheet8->write ('C6', $cell_text, $text_wrap);
  2572. $worksheet8->write_comment('C6', $comment);
  2573. __END__
  2574. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/comments2.pl>
  2575. =head2 Example: copyformat.pl
  2576. Example of how to use the format copying method with Spreadsheet::WriteExcel.
  2577. This feature isn't required very often.
  2578. =begin html
  2579. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/copyformat.jpg" width="640" height="420" alt="Output from copyformat.pl" /></center></p>
  2580. =end html
  2581. Source code for this example:
  2582. #!/usr/bin/perl -w
  2583. ###############################################################################
  2584. #
  2585. # Example of how to use the format copying method with Spreadsheet::WriteExcel.
  2586. #
  2587. # This feature isn't required very often.
  2588. #
  2589. # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org
  2590. #
  2591. use strict;
  2592. use Spreadsheet::WriteExcel;
  2593. # Create workbook1
  2594. my $workbook1 = Spreadsheet::WriteExcel->new("workbook1.xls");
  2595. my $worksheet1 = $workbook1->add_worksheet();
  2596. my $format1a = $workbook1->add_format();
  2597. my $format1b = $workbook1->add_format();
  2598. # Create workbook2
  2599. my $workbook2 = Spreadsheet::WriteExcel->new("workbook2.xls");
  2600. my $worksheet2 = $workbook2->add_worksheet();
  2601. my $format2a = $workbook2->add_format();
  2602. my $format2b = $workbook2->add_format();
  2603. # Create a global format object that isn't tied to a workbook
  2604. my $global_format = Spreadsheet::WriteExcel::Format->new();
  2605. # Set the formatting
  2606. $global_format->set_color('blue');
  2607. $global_format->set_bold();
  2608. $global_format->set_italic();
  2609. # Create another example format
  2610. $format1b->set_color('red');
  2611. # Copy the global format properties to the worksheet formats
  2612. $format1a->copy($global_format);
  2613. $format2a->copy($global_format);
  2614. # Copy a format from worksheet1 to worksheet2
  2615. $format2b->copy($format1b);
  2616. # Write some output
  2617. $worksheet1->write(0, 0, "Ciao", $format1a);
  2618. $worksheet1->write(1, 0, "Ciao", $format1b);
  2619. $worksheet2->write(0, 0, "Hello", $format2a);
  2620. $worksheet2->write(1, 0, "Hello", $format2b);
  2621. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/copyformat.pl>
  2622. =head2 Example: data_validate.pl
  2623. Example of how to add data validation and dropdown lists to a
  2624. Spreadsheet::WriteExcel file.
  2625. Data validation is a feature of Excel which allows you to restrict the data
  2626. that a users enters in a cell and to display help and warning messages. It
  2627. also allows you to restrict input to values in a drop down list.
  2628. =begin html
  2629. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/data_validate.jpg" width="640" height="420" alt="Output from data_validate.pl" /></center></p>
  2630. =end html
  2631. Source code for this example:
  2632. #!/usr/bin/perl -w
  2633. ###############################################################################
  2634. #
  2635. # Example of how to add data validation and dropdown lists to a
  2636. # Spreadsheet::WriteExcel file.
  2637. #
  2638. # Data validation is a feature of Excel which allows you to restrict the data
  2639. # that a users enters in a cell and to display help and warning messages. It
  2640. # also allows you to restrict input to values in a drop down list.
  2641. #
  2642. # reverse('©'), August 2008, John McNamara, jmcnamara@cpan.org
  2643. #
  2644. use strict;
  2645. use Spreadsheet::WriteExcel;
  2646. my $workbook = Spreadsheet::WriteExcel->new('data_validate.xls');
  2647. my $worksheet = $workbook->add_worksheet();
  2648. # Add a format for the header cells.
  2649. my $header_format = $workbook->add_format(
  2650. border => 1,
  2651. bg_color => 43,
  2652. bold => 1,
  2653. text_wrap => 1,
  2654. valign => 'vcenter',
  2655. indent => 1,
  2656. );
  2657. # Set up layout of the worksheet.
  2658. $worksheet->set_column('A:A', 64);
  2659. $worksheet->set_column('B:B', 15);
  2660. $worksheet->set_column('D:D', 15);
  2661. $worksheet->set_row(0, 36);
  2662. $worksheet->set_selection('B3');
  2663. # Write the header cells and some data that will be used in the examples.
  2664. my $row = 0;
  2665. my $txt;
  2666. my $heading1 = 'Some examples of data validation in Spreadsheet::WriteExcel';
  2667. my $heading2 = 'Enter values in this column';
  2668. my $heading3 = 'Sample Data';
  2669. $worksheet->write('A1', $heading1, $header_format);
  2670. $worksheet->write('B1', $heading2, $header_format);
  2671. $worksheet->write('D1', $heading3, $header_format);
  2672. $worksheet->write('D3', ['Integers', 1, 10]);
  2673. $worksheet->write('D4', ['List data', 'open', 'high', 'close']);
  2674. $worksheet->write('D5', ['Formula', '=AND(F5=50,G5=60)', 50, 60]);
  2675. #
  2676. # Example 1. Limiting input to an integer in a fixed range.
  2677. #
  2678. $txt = 'Enter an integer between 1 and 10';
  2679. $row += 2;
  2680. $worksheet->write($row, 0, $txt);
  2681. $worksheet->data_validation($row, 1,
  2682. {
  2683. validate => 'integer',
  2684. criteria => 'between',
  2685. minimum => 1,
  2686. maximum => 10,
  2687. });
  2688. #
  2689. # Example 2. Limiting input to an integer outside a fixed range.
  2690. #
  2691. $txt = 'Enter an integer that is not between 1 and 10 (using cell references)';
  2692. $row += 2;
  2693. $worksheet->write($row, 0, $txt);
  2694. $worksheet->data_validation($row, 1,
  2695. {
  2696. validate => 'integer',
  2697. criteria => 'not between',
  2698. minimum => '=E3',
  2699. maximum => '=F3',
  2700. });
  2701. #
  2702. # Example 3. Limiting input to an integer greater than a fixed value.
  2703. #
  2704. $txt = 'Enter an integer greater than 0';
  2705. $row += 2;
  2706. $worksheet->write($row, 0, $txt);
  2707. $worksheet->data_validation($row, 1,
  2708. {
  2709. validate => 'integer',
  2710. criteria => '>',
  2711. value => 0,
  2712. });
  2713. #
  2714. # Example 4. Limiting input to an integer less than a fixed value.
  2715. #
  2716. $txt = 'Enter an integer less than 10';
  2717. $row += 2;
  2718. $worksheet->write($row, 0, $txt);
  2719. $worksheet->data_validation($row, 1,
  2720. {
  2721. validate => 'integer',
  2722. criteria => '<',
  2723. value => 10,
  2724. });
  2725. #
  2726. # Example 5. Limiting input to a decimal in a fixed range.
  2727. #
  2728. $txt = 'Enter a decimal between 0.1 and 0.5';
  2729. $row += 2;
  2730. $worksheet->write($row, 0, $txt);
  2731. $worksheet->data_validation($row, 1,
  2732. {
  2733. validate => 'decimal',
  2734. criteria => 'between',
  2735. minimum => 0.1,
  2736. maximum => 0.5,
  2737. });
  2738. #
  2739. # Example 6. Limiting input to a value in a dropdown list.
  2740. #
  2741. $txt = 'Select a value from a drop down list';
  2742. $row += 2;
  2743. $worksheet->write($row, 0, $txt);
  2744. $worksheet->data_validation($row, 1,
  2745. {
  2746. validate => 'list',
  2747. source => ['open', 'high', 'close'],
  2748. });
  2749. #
  2750. # Example 6. Limiting input to a value in a dropdown list.
  2751. #
  2752. $txt = 'Select a value from a drop down list (using a cell range)';
  2753. $row += 2;
  2754. $worksheet->write($row, 0, $txt);
  2755. $worksheet->data_validation($row, 1,
  2756. {
  2757. validate => 'list',
  2758. source => '=E4:G4',
  2759. });
  2760. #
  2761. # Example 7. Limiting input to a date in a fixed range.
  2762. #
  2763. $txt = 'Enter a date between 1/1/2008 and 12/12/2008';
  2764. $row += 2;
  2765. $worksheet->write($row, 0, $txt);
  2766. $worksheet->data_validation($row, 1,
  2767. {
  2768. validate => 'date',
  2769. criteria => 'between',
  2770. minimum => '2008-01-01T',
  2771. maximum => '2008-12-12T',
  2772. });
  2773. #
  2774. # Example 8. Limiting input to a time in a fixed range.
  2775. #
  2776. $txt = 'Enter a time between 6:00 and 12:00';
  2777. $row += 2;
  2778. $worksheet->write($row, 0, $txt);
  2779. $worksheet->data_validation($row, 1,
  2780. {
  2781. validate => 'time',
  2782. criteria => 'between',
  2783. minimum => 'T06:00',
  2784. maximum => 'T12:00',
  2785. });
  2786. #
  2787. # Example 9. Limiting input to a string greater than a fixed length.
  2788. #
  2789. $txt = 'Enter a string longer than 3 characters';
  2790. $row += 2;
  2791. $worksheet->write($row, 0, $txt);
  2792. $worksheet->data_validation($row, 1,
  2793. {
  2794. validate => 'length',
  2795. criteria => '>',
  2796. value => 3,
  2797. });
  2798. #
  2799. # Example 10. Limiting input based on a formula.
  2800. #
  2801. $txt = 'Enter a value if the following is true "=AND(F5=50,G5=60)"';
  2802. $row += 2;
  2803. $worksheet->write($row, 0, $txt);
  2804. $worksheet->data_validation($row, 1,
  2805. {
  2806. validate => 'custom',
  2807. value => '=AND(F5=50,G5=60)',
  2808. });
  2809. #
  2810. # Example 11. Displaying and modify data validation messages.
  2811. #
  2812. $txt = 'Displays a message when you select the cell';
  2813. $row += 2;
  2814. $worksheet->write($row, 0, $txt);
  2815. $worksheet->data_validation($row, 1,
  2816. {
  2817. validate => 'integer',
  2818. criteria => 'between',
  2819. minimum => 1,
  2820. maximum => 100,
  2821. input_title => 'Enter an integer:',
  2822. input_message => 'between 1 and 100',
  2823. });
  2824. #
  2825. # Example 12. Displaying and modify data validation messages.
  2826. #
  2827. $txt = 'Display a custom error message when integer isn\'t between 1 and 100';
  2828. $row += 2;
  2829. $worksheet->write($row, 0, $txt);
  2830. $worksheet->data_validation($row, 1,
  2831. {
  2832. validate => 'integer',
  2833. criteria => 'between',
  2834. minimum => 1,
  2835. maximum => 100,
  2836. input_title => 'Enter an integer:',
  2837. input_message => 'between 1 and 100',
  2838. error_title => 'Input value is not valid!',
  2839. error_message => 'It should be an integer between 1 and 100',
  2840. });
  2841. #
  2842. # Example 13. Displaying and modify data validation messages.
  2843. #
  2844. $txt = 'Display a custom information message when integer isn\'t between 1 and 100';
  2845. $row += 2;
  2846. $worksheet->write($row, 0, $txt);
  2847. $worksheet->data_validation($row, 1,
  2848. {
  2849. validate => 'integer',
  2850. criteria => 'between',
  2851. minimum => 1,
  2852. maximum => 100,
  2853. input_title => 'Enter an integer:',
  2854. input_message => 'between 1 and 100',
  2855. error_title => 'Input value is not valid!',
  2856. error_message => 'It should be an integer between 1 and 100',
  2857. error_type => 'information',
  2858. });
  2859. __END__
  2860. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/data_validate.pl>
  2861. =head2 Example: date_time.pl
  2862. Spreadsheet::WriteExcel example of writing dates and times using the
  2863. write_date_time() Worksheet method.
  2864. =begin html
  2865. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/date_time.jpg" width="640" height="420" alt="Output from date_time.pl" /></center></p>
  2866. =end html
  2867. Source code for this example:
  2868. #!/usr/bin/perl -w
  2869. ###############################################################################
  2870. #
  2871. # Spreadsheet::WriteExcel example of writing dates and times using the
  2872. # write_date_time() Worksheet method.
  2873. #
  2874. # reverse('©'), August 2004, John McNamara, jmcnamara@cpan.org
  2875. #
  2876. use strict;
  2877. use Spreadsheet::WriteExcel;
  2878. # Create a new workbook and add a worksheet
  2879. my $workbook = Spreadsheet::WriteExcel->new("date_time.xls");
  2880. my $worksheet = $workbook->add_worksheet();
  2881. my $bold = $workbook->add_format(bold => 1);
  2882. my $row = 0;
  2883. # Expand the first column so that the date is visible.
  2884. $worksheet->set_column("A:B", 30);
  2885. # Write the column headers
  2886. $worksheet->write('A1', 'Formatted date', $bold);
  2887. $worksheet->write('B1', 'Format', $bold);
  2888. # Examples date and time formats. In the output file compare how changing
  2889. # the format codes change the appearance of the date.
  2890. #
  2891. my @date_formats = (
  2892. 'dd/mm/yy',
  2893. 'mm/dd/yy',
  2894. '',
  2895. 'd mm yy',
  2896. 'dd mm yy',
  2897. '',
  2898. 'dd m yy',
  2899. 'dd mm yy',
  2900. 'dd mmm yy',
  2901. 'dd mmmm yy',
  2902. '',
  2903. 'dd mm y',
  2904. 'dd mm yyy',
  2905. 'dd mm yyyy',
  2906. '',
  2907. 'd mmmm yyyy',
  2908. '',
  2909. 'dd/mm/yy',
  2910. 'dd/mm/yy hh:mm',
  2911. 'dd/mm/yy hh:mm:ss',
  2912. 'dd/mm/yy hh:mm:ss.000',
  2913. '',
  2914. 'hh:mm',
  2915. 'hh:mm:ss',
  2916. 'hh:mm:ss.000',
  2917. );
  2918. # Write the same date and time using each of the above formats. The empty
  2919. # string formats create a blank line to make the example clearer.
  2920. #
  2921. for my $date_format (@date_formats) {
  2922. $row++;
  2923. next if $date_format eq '';
  2924. # Create a format for the date or time.
  2925. my $format = $workbook->add_format(
  2926. num_format => $date_format,
  2927. align => 'left'
  2928. );
  2929. # Write the same date using different formats.
  2930. $worksheet->write_date_time($row, 0, '2004-08-01T12:30:45.123', $format);
  2931. $worksheet->write ($row, 1, $date_format);
  2932. }
  2933. # The following is an example of an invalid date. It is written as a string
  2934. # instead of a number. This is also Excel's default behaviour.
  2935. #
  2936. $row += 2;
  2937. $worksheet->write_date_time($row, 0, '2004-13-01T12:30:45.123');
  2938. $worksheet->write ($row, 1, 'Invalid date. Written as string.', $bold);
  2939. __END__
  2940. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/date_time.pl>
  2941. =head2 Example: defined_name.pl
  2942. Example of how to create defined names in a Spreadsheet::WriteExcel file.
  2943. This method is used to defined a name that can be used to represent a value,
  2944. a single cell or a range of cells in a workbook.
  2945. =begin html
  2946. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/defined_name.jpg" width="640" height="420" alt="Output from defined_name.pl" /></center></p>
  2947. =end html
  2948. Source code for this example:
  2949. #!/usr/bin/perl -w
  2950. ###############################################################################
  2951. #
  2952. # Example of how to create defined names in a Spreadsheet::WriteExcel file.
  2953. #
  2954. # This method is used to defined a name that can be used to represent a value,
  2955. # a single cell or a range of cells in a workbook.
  2956. #
  2957. # reverse('©'), September 2008, John McNamara, jmcnamara@cpan.org
  2958. #
  2959. use strict;
  2960. use Spreadsheet::WriteExcel;
  2961. my $workbook = Spreadsheet::WriteExcel->new('defined_name.xls');
  2962. my $worksheet1 = $workbook->add_worksheet();
  2963. my $worksheet2 = $workbook->add_worksheet();
  2964. $workbook->define_name('Exchange_rate', '=0.96');
  2965. $workbook->define_name('Sales', '=Sheet1!$G$1:$H$10');
  2966. $workbook->define_name('Sheet2!Sales', '=Sheet2!$G$1:$G$10');
  2967. for my $worksheet ($workbook->sheets()) {
  2968. $worksheet->set_column('A:A', 45);
  2969. $worksheet->write('A2', 'This worksheet contains some defined names,');
  2970. $worksheet->write('A3', 'See the Insert -> Name -> Define dialog.');
  2971. }
  2972. $worksheet1->write('A4', '=Exchange_rate');
  2973. __END__
  2974. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/defined_name.pl>
  2975. =head2 Example: diag_border.pl
  2976. A simple formatting example that demonstrates how to add a diagonal cell
  2977. border with Spreadsheet::WriteExcel
  2978. =begin html
  2979. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/diag_border.jpg" width="640" height="420" alt="Output from diag_border.pl" /></center></p>
  2980. =end html
  2981. Source code for this example:
  2982. #!/usr/bin/perl -w
  2983. ##############################################################################
  2984. #
  2985. # A simple formatting example that demonstrates how to add a diagonal cell
  2986. # border with Spreadsheet::WriteExcel
  2987. #
  2988. # reverse('©'), May 2004, John McNamara, jmcnamara@cpan.org
  2989. #
  2990. use strict;
  2991. use Spreadsheet::WriteExcel;
  2992. my $workbook = Spreadsheet::WriteExcel->new('diag_border.xls');
  2993. my $worksheet = $workbook->add_worksheet();
  2994. my $format1 = $workbook->add_format(diag_type => '1');
  2995. my $format2 = $workbook->add_format(diag_type => '2');
  2996. my $format3 = $workbook->add_format(diag_type => '3');
  2997. my $format4 = $workbook->add_format(
  2998. diag_type => '3',
  2999. diag_border => '7',
  3000. diag_color => 'red',
  3001. );
  3002. $worksheet->write('B3', 'Text', $format1);
  3003. $worksheet->write('B6', 'Text', $format2);
  3004. $worksheet->write('B9', 'Text', $format3);
  3005. $worksheet->write('B12', 'Text', $format4);
  3006. __END__
  3007. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/diag_border.pl>
  3008. =head2 Example: easter_egg.pl
  3009. This uses the Win32::OLE module to expose the Flight Simulator easter egg
  3010. in Excel 97 SR2.
  3011. #!/usr/bin/perl -w
  3012. ###############################################################################
  3013. #
  3014. # This uses the Win32::OLE module to expose the Flight Simulator easter egg
  3015. # in Excel 97 SR2.
  3016. #
  3017. # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org
  3018. #
  3019. use strict;
  3020. use Win32::OLE;
  3021. my $application = Win32::OLE->new("Excel.Application");
  3022. my $workbook = $application->Workbooks->Add;
  3023. my $worksheet = $workbook->Worksheets(1);
  3024. $application->{Visible} = 1;
  3025. $worksheet->Range("L97:X97")->Select;
  3026. $worksheet->Range("M97")->Activate;
  3027. my $message = "Hold down Shift and Ctrl and click the ".
  3028. "Chart Wizard icon on the toolbar.\n\n".
  3029. "Use the mouse motion and buttons to control ".
  3030. "movement. Try to find the monolith. ".
  3031. "Close this dialog first.";
  3032. $application->InputBox($message);
  3033. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/easter_egg.pl>
  3034. =head2 Example: filehandle.pl
  3035. Example of using Spreadsheet::WriteExcel to write Excel files to
  3036. different filehandles.
  3037. =begin html
  3038. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/filehandle.jpg" width="640" height="420" alt="Output from filehandle.pl" /></center></p>
  3039. =end html
  3040. Source code for this example:
  3041. #!/usr/bin/perl -w
  3042. ###############################################################################
  3043. #
  3044. # Example of using Spreadsheet::WriteExcel to write Excel files to
  3045. # different filehandles.
  3046. #
  3047. # reverse('©'), April 2003, John McNamara, jmcnamara@cpan.org
  3048. #
  3049. use strict;
  3050. use Spreadsheet::WriteExcel;
  3051. use IO::Scalar;
  3052. ###############################################################################
  3053. #
  3054. # Example 1. This demonstrates the standard way of creating an Excel file by
  3055. # specifying a file name.
  3056. #
  3057. my $workbook1 = Spreadsheet::WriteExcel->new('fh_01.xls');
  3058. my $worksheet1 = $workbook1->add_worksheet();
  3059. $worksheet1->write(0, 0, "Hi Excel!");
  3060. ###############################################################################
  3061. #
  3062. # Example 2. Write an Excel file to an existing filehandle.
  3063. #
  3064. open TEST, "> fh_02.xls" or die "Couldn't open file: $!";
  3065. binmode TEST; # Always do this regardless of whether the platform requires it.
  3066. my $workbook2 = Spreadsheet::WriteExcel->new(\*TEST);
  3067. my $worksheet2 = $workbook2->add_worksheet();
  3068. $worksheet2->write(0, 0, "Hi Excel!");
  3069. ###############################################################################
  3070. #
  3071. # Example 3. Write an Excel file to an existing OO style filehandle.
  3072. #
  3073. my $fh = FileHandle->new("> fh_03.xls")
  3074. or die "Couldn't open file: $!";
  3075. binmode($fh);
  3076. my $workbook3 = Spreadsheet::WriteExcel->new($fh);
  3077. my $worksheet3 = $workbook3->add_worksheet();
  3078. $worksheet3->write(0, 0, "Hi Excel!");
  3079. ###############################################################################
  3080. #
  3081. # Example 4. Write an Excel file to a string via IO::Scalar. Please refer to
  3082. # the IO::Scalar documentation for further details.
  3083. #
  3084. my $xls_str;
  3085. tie *XLS, 'IO::Scalar', \$xls_str;
  3086. my $workbook4 = Spreadsheet::WriteExcel->new(\*XLS);
  3087. my $worksheet4 = $workbook4->add_worksheet();
  3088. $worksheet4->write(0, 0, "Hi Excel 4");
  3089. $workbook4->close(); # This is required before we use the scalar
  3090. # The Excel file is now in $xls_str. As a demonstration, print it to a file.
  3091. open TMP, "> fh_04.xls" or die "Couldn't open file: $!";
  3092. binmode TMP;
  3093. print TMP $xls_str;
  3094. close TMP;
  3095. ###############################################################################
  3096. #
  3097. # Example 5. Write an Excel file to a string via IO::Scalar's newer interface.
  3098. # Please refer to the IO::Scalar documentation for further details.
  3099. #
  3100. my $xls_str2;
  3101. my $fh5 = IO::Scalar->new(\$xls_str2);
  3102. my $workbook5 = Spreadsheet::WriteExcel->new($fh5);
  3103. my $worksheet5 = $workbook5->add_worksheet();
  3104. $worksheet5->write(0, 0, "Hi Excel 5");
  3105. $workbook5->close(); # This is required before we use the scalar
  3106. # The Excel file is now in $xls_str. As a demonstration, print it to a file.
  3107. open TMP, "> fh_05.xls" or die "Couldn't open file: $!";
  3108. binmode TMP;
  3109. print TMP $xls_str2;
  3110. close TMP;
  3111. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/filehandle.pl>
  3112. =head2 Example: formula_result.pl
  3113. Example of how to write Spreadsheet::WriteExcel formulas with a user
  3114. specified result.
  3115. This is generally only required when writing a spreadsheet for an
  3116. application other than Excel where the formula isn't evaluated.
  3117. =begin html
  3118. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/formula_result.jpg" width="640" height="420" alt="Output from formula_result.pl" /></center></p>
  3119. =end html
  3120. Source code for this example:
  3121. #!/usr/bin/perl -w
  3122. #######################################################################
  3123. #
  3124. # Example of how to write Spreadsheet::WriteExcel formulas with a user
  3125. # specified result.
  3126. #
  3127. # This is generally only required when writing a spreadsheet for an
  3128. # application other than Excel where the formula isn't evaluated.
  3129. #
  3130. # reverse('©'), August 2005, John McNamara, jmcnamara@cpan.org
  3131. #
  3132. use strict;
  3133. use Spreadsheet::WriteExcel;
  3134. my $workbook = Spreadsheet::WriteExcel->new('formula_result.xls');
  3135. my $worksheet = $workbook->add_worksheet();
  3136. my $format = $workbook->add_format(color => 'blue');
  3137. $worksheet->write('A1', '=1+2');
  3138. $worksheet->write('A2', '=1+2', $format, 4);
  3139. $worksheet->write('A3', '="ABC"', undef, 'DEF');
  3140. $worksheet->write('A4', '=IF(A1 > 1, TRUE, FALSE)', undef, 'TRUE');
  3141. $worksheet->write('A5', '=1/0', undef, '#DIV/0!');
  3142. __END__
  3143. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/formula_result.pl>
  3144. =head2 Example: headers.pl
  3145. This program shows several examples of how to set up headers and
  3146. footers with Spreadsheet::WriteExcel.
  3147. The control characters used in the header/footer strings are:
  3148. Control Category Description
  3149. ======= ======== ===========
  3150. &L Justification Left
  3151. &C Center
  3152. &R Right
  3153. &P Information Page number
  3154. &N Total number of pages
  3155. &D Date
  3156. &T Time
  3157. &F File name
  3158. &A Worksheet name
  3159. &fontsize Font Font size
  3160. &"font,style" Font name and style
  3161. &U Single underline
  3162. &E Double underline
  3163. &S Strikethrough
  3164. &X Superscript
  3165. &Y Subscript
  3166. && Miscellaneous Literal ampersand &
  3167. See the main Spreadsheet::WriteExcel documentation for more information.
  3168. =begin html
  3169. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/headers.jpg" width="640" height="420" alt="Output from headers.pl" /></center></p>
  3170. =end html
  3171. Source code for this example:
  3172. #!/usr/bin/perl -w
  3173. ######################################################################
  3174. #
  3175. # This program shows several examples of how to set up headers and
  3176. # footers with Spreadsheet::WriteExcel.
  3177. #
  3178. # The control characters used in the header/footer strings are:
  3179. #
  3180. # Control Category Description
  3181. # ======= ======== ===========
  3182. # &L Justification Left
  3183. # &C Center
  3184. # &R Right
  3185. #
  3186. # &P Information Page number
  3187. # &N Total number of pages
  3188. # &D Date
  3189. # &T Time
  3190. # &F File name
  3191. # &A Worksheet name
  3192. #
  3193. # &fontsize Font Font size
  3194. # &"font,style" Font name and style
  3195. # &U Single underline
  3196. # &E Double underline
  3197. # &S Strikethrough
  3198. # &X Superscript
  3199. # &Y Subscript
  3200. #
  3201. # && Miscellaneous Literal ampersand &
  3202. #
  3203. # See the main Spreadsheet::WriteExcel documentation for more information.
  3204. #
  3205. # reverse('©'), March 2002, John McNamara, jmcnamara@cpan.org
  3206. #
  3207. use strict;
  3208. use Spreadsheet::WriteExcel;
  3209. my $workbook = Spreadsheet::WriteExcel->new("headers.xls");
  3210. my $preview = "Select Print Preview to see the header and footer";
  3211. ######################################################################
  3212. #
  3213. # A simple example to start
  3214. #
  3215. my $worksheet1 = $workbook->add_worksheet('Simple');
  3216. my $header1 = '&CHere is some centred text.';
  3217. my $footer1 = '&LHere is some left aligned text.';
  3218. $worksheet1->set_header($header1);
  3219. $worksheet1->set_footer($footer1);
  3220. $worksheet1->set_column('A:A', 50);
  3221. $worksheet1->write('A1', $preview);
  3222. ######################################################################
  3223. #
  3224. # This is an example of some of the header/footer variables.
  3225. #
  3226. my $worksheet2 = $workbook->add_worksheet('Variables');
  3227. my $header2 = '&LPage &P of &N'.
  3228. '&CFilename: &F' .
  3229. '&RSheetname: &A';
  3230. my $footer2 = '&LCurrent date: &D'.
  3231. '&RCurrent time: &T';
  3232. $worksheet2->set_header($header2);
  3233. $worksheet2->set_footer($footer2);
  3234. $worksheet2->set_column('A:A', 50);
  3235. $worksheet2->write('A1', $preview);
  3236. $worksheet2->write('A21', "Next sheet");
  3237. $worksheet2->set_h_pagebreaks(20);
  3238. ######################################################################
  3239. #
  3240. # This example shows how to use more than one font
  3241. #
  3242. my $worksheet3 = $workbook->add_worksheet('Mixed fonts');
  3243. my $header3 = '&C' .
  3244. '&"Courier New,Bold"Hello ' .
  3245. '&"Arial,Italic"World';
  3246. my $footer3 = '&C' .
  3247. '&"Symbol"e' .
  3248. '&"Arial" = mc&X2';
  3249. $worksheet3->set_header($header3);
  3250. $worksheet3->set_footer($footer3);
  3251. $worksheet3->set_column('A:A', 50);
  3252. $worksheet3->write('A1', $preview);
  3253. ######################################################################
  3254. #
  3255. # Example of line wrapping
  3256. #
  3257. my $worksheet4 = $workbook->add_worksheet('Word wrap');
  3258. my $header4 = "&CHeading 1\nHeading 2\nHeading 3";
  3259. $worksheet4->set_header($header4);
  3260. $worksheet4->set_column('A:A', 50);
  3261. $worksheet4->write('A1', $preview);
  3262. ######################################################################
  3263. #
  3264. # Example of inserting a literal ampersand &
  3265. #
  3266. my $worksheet5 = $workbook->add_worksheet('Ampersand');
  3267. my $header5 = "&CCuriouser && Curiouser - Attorneys at Law";
  3268. $worksheet5->set_header($header5);
  3269. $worksheet5->set_column('A:A', 50);
  3270. $worksheet5->write('A1', $preview);
  3271. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/headers.pl>
  3272. =head2 Example: hide_sheet.pl
  3273. Example of how to hide a worksheet with Spreadsheet::WriteExcel.
  3274. =begin html
  3275. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/hide_sheet.jpg" width="640" height="420" alt="Output from hide_sheet.pl" /></center></p>
  3276. =end html
  3277. Source code for this example:
  3278. #!/usr/bin/perl -w
  3279. #######################################################################
  3280. #
  3281. # Example of how to hide a worksheet with Spreadsheet::WriteExcel.
  3282. #
  3283. # reverse('©'), April 2005, John McNamara, jmcnamara@cpan.org
  3284. #
  3285. use strict;
  3286. use Spreadsheet::WriteExcel;
  3287. my $workbook = Spreadsheet::WriteExcel->new('hidden.xls');
  3288. my $worksheet1 = $workbook->add_worksheet();
  3289. my $worksheet2 = $workbook->add_worksheet();
  3290. my $worksheet3 = $workbook->add_worksheet();
  3291. # Sheet2 won't be visible until it is unhidden in Excel.
  3292. $worksheet2->hide();
  3293. $worksheet1->write(0, 0, 'Sheet2 is hidden');
  3294. $worksheet2->write(0, 0, 'How did you find me?');
  3295. $worksheet3->write(0, 0, 'Sheet2 is hidden');
  3296. __END__
  3297. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/hide_sheet.pl>
  3298. =head2 Example: hyperlink1.pl
  3299. Example of how to use the WriteExcel module to write hyperlinks.
  3300. See also hyperlink2.pl for worksheet URL examples.
  3301. =begin html
  3302. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/hyperlink1.jpg" width="640" height="420" alt="Output from hyperlink1.pl" /></center></p>
  3303. =end html
  3304. Source code for this example:
  3305. #!/usr/bin/perl -w
  3306. ###############################################################################
  3307. #
  3308. # Example of how to use the WriteExcel module to write hyperlinks.
  3309. #
  3310. # See also hyperlink2.pl for worksheet URL examples.
  3311. #
  3312. # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org
  3313. #
  3314. use strict;
  3315. use Spreadsheet::WriteExcel;
  3316. # Create a new workbook and add a worksheet
  3317. my $workbook = Spreadsheet::WriteExcel->new("hyperlink.xls");
  3318. my $worksheet = $workbook->add_worksheet('Hyperlinks');
  3319. # Format the first column
  3320. $worksheet->set_column('A:A', 30);
  3321. $worksheet->set_selection('B1');
  3322. # Add a sample format
  3323. my $format = $workbook->add_format();
  3324. $format->set_size(12);
  3325. $format->set_bold();
  3326. $format->set_color('red');
  3327. $format->set_underline();
  3328. # Write some hyperlinks
  3329. $worksheet->write('A1', 'http://www.perl.com/' );
  3330. $worksheet->write('A3', 'http://www.perl.com/', 'Perl home' );
  3331. $worksheet->write('A5', 'http://www.perl.com/', undef, $format);
  3332. $worksheet->write('A7', 'mailto:jmcnamara@cpan.org', 'Mail me');
  3333. # Write a URL that isn't a hyperlink
  3334. $worksheet->write_string('A9', 'http://www.perl.com/');
  3335. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/hyperlink1.pl>
  3336. =head2 Example: hyperlink2.pl
  3337. Example of how to use the WriteExcel module to write internal and internal
  3338. hyperlinks.
  3339. If you wish to run this program and follow the hyperlinks you should create
  3340. the following directory structure:
  3341. C:\ -- Temp --+-- Europe
  3342. |
  3343. \-- Asia
  3344. See also hyperlink1.pl for web URL examples.
  3345. #!/usr/bin/perl -w
  3346. ###############################################################################
  3347. #
  3348. # Example of how to use the WriteExcel module to write internal and internal
  3349. # hyperlinks.
  3350. #
  3351. # If you wish to run this program and follow the hyperlinks you should create
  3352. # the following directory structure:
  3353. #
  3354. # C:\ -- Temp --+-- Europe
  3355. # |
  3356. # \-- Asia
  3357. #
  3358. #
  3359. # See also hyperlink1.pl for web URL examples.
  3360. #
  3361. # reverse('©'), February 2002, John McNamara, jmcnamara@cpan.org
  3362. #
  3363. use strict;
  3364. use Spreadsheet::WriteExcel;
  3365. # Create three workbooks:
  3366. # C:\Temp\Europe\Ireland.xls
  3367. # C:\Temp\Europe\Italy.xls
  3368. # C:\Temp\Asia\China.xls
  3369. #
  3370. my $ireland = Spreadsheet::WriteExcel->new('C:\Temp\Europe\Ireland.xls');
  3371. my $ire_links = $ireland->add_worksheet('Links');
  3372. my $ire_sales = $ireland->add_worksheet('Sales');
  3373. my $ire_data = $ireland->add_worksheet('Product Data');
  3374. my $italy = Spreadsheet::WriteExcel->new('C:\Temp\Europe\Italy.xls');
  3375. my $ita_links = $italy->add_worksheet('Links');
  3376. my $ita_sales = $italy->add_worksheet('Sales');
  3377. my $ita_data = $italy->add_worksheet('Product Data');
  3378. my $china = Spreadsheet::WriteExcel->new('C:\Temp\Asia\China.xls');
  3379. my $cha_links = $china->add_worksheet('Links');
  3380. my $cha_sales = $china->add_worksheet('Sales');
  3381. my $cha_data = $china->add_worksheet('Product Data');
  3382. # Add a format
  3383. my $format = $ireland->add_format(color => 'green', bold => 1);
  3384. $ire_links->set_column('A:B', 25);
  3385. ###############################################################################
  3386. #
  3387. # Examples of internal links
  3388. #
  3389. $ire_links->write('A1', 'Internal links', $format);
  3390. # Internal link
  3391. $ire_links->write('A2', 'internal:Sales!A2');
  3392. # Internal link to a range
  3393. $ire_links->write('A3', 'internal:Sales!A3:D3');
  3394. # Internal link with an alternative string
  3395. $ire_links->write('A4', 'internal:Sales!A4', 'Link');
  3396. # Internal link with a format
  3397. $ire_links->write('A5', 'internal:Sales!A5', $format);
  3398. # Internal link with an alternative string and format
  3399. $ire_links->write('A6', 'internal:Sales!A6', 'Link', $format);
  3400. # Internal link (spaces in worksheet name)
  3401. $ire_links->write('A7', q{internal:'Product Data'!A7});
  3402. ###############################################################################
  3403. #
  3404. # Examples of external links
  3405. #
  3406. $ire_links->write('B1', 'External links', $format);
  3407. # External link to a local file
  3408. $ire_links->write('B2', 'external:Italy.xls');
  3409. # External link to a local file with worksheet
  3410. $ire_links->write('B3', 'external:Italy.xls#Sales!B3');
  3411. # External link to a local file with worksheet and alternative string
  3412. $ire_links->write('B4', 'external:Italy.xls#Sales!B4', 'Link');
  3413. # External link to a local file with worksheet and format
  3414. $ire_links->write('B5', 'external:Italy.xls#Sales!B5', $format);
  3415. # External link to a remote file, absolute path
  3416. $ire_links->write('B6', 'external:c:/Temp/Asia/China.xls');
  3417. # External link to a remote file, relative path
  3418. $ire_links->write('B7', 'external:../Asia/China.xls');
  3419. # External link to a remote file with worksheet
  3420. $ire_links->write('B8', 'external:c:/Temp/Asia/China.xls#Sales!B8');
  3421. # External link to a remote file with worksheet (with spaces in the name)
  3422. $ire_links->write('B9', q{external:c:/Temp/Asia/China.xls#'Product Data'!B9});
  3423. ###############################################################################
  3424. #
  3425. # Some utility links to return to the main sheet
  3426. #
  3427. $ire_sales->write('A2', 'internal:Links!A2', 'Back');
  3428. $ire_sales->write('A3', 'internal:Links!A3', 'Back');
  3429. $ire_sales->write('A4', 'internal:Links!A4', 'Back');
  3430. $ire_sales->write('A5', 'internal:Links!A5', 'Back');
  3431. $ire_sales->write('A6', 'internal:Links!A6', 'Back');
  3432. $ire_data-> write('A7', 'internal:Links!A7', 'Back');
  3433. $ita_links->write('A1', 'external:Ireland.xls#Links!B2', 'Back');
  3434. $ita_sales->write('B3', 'external:Ireland.xls#Links!B3', 'Back');
  3435. $ita_sales->write('B4', 'external:Ireland.xls#Links!B4', 'Back');
  3436. $ita_sales->write('B5', 'external:Ireland.xls#Links!B5', 'Back');
  3437. $cha_links->write('A1', 'external:../Europe/Ireland.xls#Links!B6', 'Back');
  3438. $cha_sales->write('B8', 'external:../Europe/Ireland.xls#Links!B8', 'Back');
  3439. $cha_data-> write('B9', 'external:../Europe/Ireland.xls#Links!B9', 'Back');
  3440. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/hyperlink2.pl>
  3441. =head2 Example: images.pl
  3442. Example of how to insert images into an Excel worksheet using the
  3443. Spreadsheet::WriteExcel insert_image() method.
  3444. =begin html
  3445. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/images.jpg" width="640" height="420" alt="Output from images.pl" /></center></p>
  3446. =end html
  3447. Source code for this example:
  3448. #!/usr/bin/perl -w
  3449. #######################################################################
  3450. #
  3451. # Example of how to insert images into an Excel worksheet using the
  3452. # Spreadsheet::WriteExcel insert_image() method.
  3453. #
  3454. # reverse('©'), October 2001, John McNamara, jmcnamara@cpan.org
  3455. #
  3456. use strict;
  3457. use Spreadsheet::WriteExcel;
  3458. # Create a new workbook called simple.xls and add a worksheet
  3459. my $workbook = Spreadsheet::WriteExcel->new("images.xls");
  3460. my $worksheet1 = $workbook->add_worksheet('Image 1');
  3461. my $worksheet2 = $workbook->add_worksheet('Image 2');
  3462. my $worksheet3 = $workbook->add_worksheet('Image 3');
  3463. my $worksheet4 = $workbook->add_worksheet('Image 4');
  3464. # Insert a basic image
  3465. $worksheet1->write('A10', "Image inserted into worksheet.");
  3466. $worksheet1->insert_image('A1', 'republic.png');
  3467. # Insert an image with an offset
  3468. $worksheet2->write('A10', "Image inserted with an offset.");
  3469. $worksheet2->insert_image('A1', 'republic.png', 32, 10);
  3470. # Insert a scaled image
  3471. $worksheet3->write('A10', "Image scaled: width x 2, height x 0.8.");
  3472. $worksheet3->insert_image('A1', 'republic.png', 0, 0, 2, 0.8);
  3473. # Insert an image over varied column and row sizes
  3474. # This does not require any additional work
  3475. # Set the cols and row sizes
  3476. # NOTE: you must do this before you call insert_image()
  3477. $worksheet4->set_column('A:A', 5);
  3478. $worksheet4->set_column('B:B', undef, undef, 1); # Hidden
  3479. $worksheet4->set_column('C:D', 10);
  3480. $worksheet4->set_row(0, 30);
  3481. $worksheet4->set_row(3, 5);
  3482. $worksheet4->write('A10', "Image inserted over scaled rows and columns.");
  3483. $worksheet4->insert_image('A1', 'republic.png');
  3484. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/images.pl>
  3485. =head2 Example: indent.pl
  3486. A simple formatting example using Spreadsheet::WriteExcel.
  3487. This program demonstrates the indentation cell format.
  3488. =begin html
  3489. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/indent.jpg" width="640" height="420" alt="Output from indent.pl" /></center></p>
  3490. =end html
  3491. Source code for this example:
  3492. #!/usr/bin/perl -w
  3493. ##############################################################################
  3494. #
  3495. # A simple formatting example using Spreadsheet::WriteExcel.
  3496. #
  3497. # This program demonstrates the indentation cell format.
  3498. #
  3499. # reverse('©'), May 2004, John McNamara, jmcnamara@cpan.org
  3500. #
  3501. use strict;
  3502. use Spreadsheet::WriteExcel;
  3503. my $workbook = Spreadsheet::WriteExcel->new('indent.xls');
  3504. my $worksheet = $workbook->add_worksheet();
  3505. my $indent1 = $workbook->add_format(indent => 1);
  3506. my $indent2 = $workbook->add_format(indent => 2);
  3507. $worksheet->set_column('A:A', 40);
  3508. $worksheet->write('A1', "This text is indented 1 level", $indent1);
  3509. $worksheet->write('A2', "This text is indented 2 levels", $indent2);
  3510. __END__
  3511. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/indent.pl>
  3512. =head2 Example: merge1.pl
  3513. Simple example of merging cells using the Spreadsheet::WriteExcel module.
  3514. This example merges three cells using the "Centre Across Selection"
  3515. alignment which was the Excel 5 method of achieving a merge. For a more
  3516. modern approach use the merge_range() worksheet method instead.
  3517. See the merge3.pl - merge6.pl programs.
  3518. =begin html
  3519. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/merge1.jpg" width="640" height="420" alt="Output from merge1.pl" /></center></p>
  3520. =end html
  3521. Source code for this example:
  3522. #!/usr/bin/perl -w
  3523. ###############################################################################
  3524. #
  3525. # Simple example of merging cells using the Spreadsheet::WriteExcel module.
  3526. #
  3527. # This example merges three cells using the "Centre Across Selection"
  3528. # alignment which was the Excel 5 method of achieving a merge. For a more
  3529. # modern approach use the merge_range() worksheet method instead.
  3530. # See the merge3.pl - merge6.pl programs.
  3531. #
  3532. # reverse('©'), August 2002, John McNamara, jmcnamara@cpan.org
  3533. #
  3534. use strict;
  3535. use Spreadsheet::WriteExcel;
  3536. # Create a new workbook and add a worksheet
  3537. my $workbook = Spreadsheet::WriteExcel->new("merge1.xls");
  3538. my $worksheet = $workbook->add_worksheet();
  3539. # Increase the cell size of the merged cells to highlight the formatting.
  3540. $worksheet->set_column('B:D', 20);
  3541. $worksheet->set_row(2, 30);
  3542. # Create a merge format
  3543. my $format = $workbook->add_format(center_across => 1);
  3544. # Only one cell should contain text, the others should be blank.
  3545. $worksheet->write (2, 1, "Center across selection", $format);
  3546. $worksheet->write_blank(2, 2, $format);
  3547. $worksheet->write_blank(2, 3, $format);
  3548. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/merge1.pl>
  3549. =head2 Example: merge2.pl
  3550. Simple example of merging cells using the Spreadsheet::WriteExcel module
  3551. This example merges three cells using the "Centre Across Selection"
  3552. alignment which was the Excel 5 method of achieving a merge. For a more
  3553. modern approach use the merge_range() worksheet method instead.
  3554. See the merge3.pl - merge6.pl programs.
  3555. =begin html
  3556. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/merge2.jpg" width="640" height="420" alt="Output from merge2.pl" /></center></p>
  3557. =end html
  3558. Source code for this example:
  3559. #!/usr/bin/perl -w
  3560. ###############################################################################
  3561. #
  3562. # Simple example of merging cells using the Spreadsheet::WriteExcel module
  3563. #
  3564. # This example merges three cells using the "Centre Across Selection"
  3565. # alignment which was the Excel 5 method of achieving a merge. For a more
  3566. # modern approach use the merge_range() worksheet method instead.
  3567. # See the merge3.pl - merge6.pl programs.
  3568. #
  3569. # reverse('©'), August 2002, John McNamara, jmcnamara@cpan.org
  3570. #
  3571. use strict;
  3572. use Spreadsheet::WriteExcel;
  3573. # Create a new workbook and add a worksheet
  3574. my $workbook = Spreadsheet::WriteExcel->new("merge2.xls");
  3575. my $worksheet = $workbook->add_worksheet();
  3576. # Increase the cell size of the merged cells to highlight the formatting.
  3577. $worksheet->set_column(1, 2, 30);
  3578. $worksheet->set_row(2, 40);
  3579. # Create a merged format
  3580. my $format = $workbook->add_format(
  3581. center_across => 1,
  3582. bold => 1,
  3583. size => 15,
  3584. pattern => 1,
  3585. border => 6,
  3586. color => 'white',
  3587. fg_color => 'green',
  3588. border_color => 'yellow',
  3589. align => 'vcenter',
  3590. );
  3591. # Only one cell should contain text, the others should be blank.
  3592. $worksheet->write (2, 1, "Center across selection", $format);
  3593. $worksheet->write_blank(2, 2, $format);
  3594. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/merge2.pl>
  3595. =head2 Example: merge3.pl
  3596. Example of how to use Spreadsheet::WriteExcel to write a hyperlink in a
  3597. merged cell. There are two options write_url_range() with a standard merge
  3598. format or merge_range().
  3599. =begin html
  3600. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/merge3.jpg" width="640" height="420" alt="Output from merge3.pl" /></center></p>
  3601. =end html
  3602. Source code for this example:
  3603. #!/usr/bin/perl -w
  3604. ###############################################################################
  3605. #
  3606. # Example of how to use Spreadsheet::WriteExcel to write a hyperlink in a
  3607. # merged cell. There are two options write_url_range() with a standard merge
  3608. # format or merge_range().
  3609. #
  3610. # reverse('©'), September 2002, John McNamara, jmcnamara@cpan.org
  3611. #
  3612. use strict;
  3613. use Spreadsheet::WriteExcel;
  3614. # Create a new workbook and add a worksheet
  3615. my $workbook = Spreadsheet::WriteExcel->new('merge3.xls');
  3616. my $worksheet = $workbook->add_worksheet();
  3617. # Increase the cell size of the merged cells to highlight the formatting.
  3618. $worksheet->set_row($_, 30) for (1, 3, 6, 7);
  3619. $worksheet->set_column('B:D', 20);
  3620. ###############################################################################
  3621. #
  3622. # Example 1: Merge cells containing a hyperlink using write_url_range()
  3623. # and the standard Excel 5+ merge property.
  3624. #
  3625. my $format1 = $workbook->add_format(
  3626. center_across => 1,
  3627. border => 1,
  3628. underline => 1,
  3629. color => 'blue',
  3630. );
  3631. # Write the cells to be merged
  3632. $worksheet->write_url_range('B2:D2', 'http://www.perl.com', $format1);
  3633. $worksheet->write_blank('C2', $format1);
  3634. $worksheet->write_blank('D2', $format1);
  3635. ###############################################################################
  3636. #
  3637. # Example 2: Merge cells containing a hyperlink using merge_range().
  3638. #
  3639. my $format2 = $workbook->add_format(
  3640. border => 1,
  3641. underline => 1,
  3642. color => 'blue',
  3643. align => 'center',
  3644. valign => 'vcenter',
  3645. );
  3646. # Merge 3 cells
  3647. $worksheet->merge_range('B4:D4', 'http://www.perl.com', $format2);
  3648. # Merge 3 cells over two rows
  3649. $worksheet->merge_range('B7:D8', 'http://www.perl.com', $format2);
  3650. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/merge3.pl>
  3651. =head2 Example: merge4.pl
  3652. Example of how to use the Spreadsheet::WriteExcel merge_range() workbook
  3653. method with complex formatting.
  3654. =begin html
  3655. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/merge4.jpg" width="640" height="420" alt="Output from merge4.pl" /></center></p>
  3656. =end html
  3657. Source code for this example:
  3658. #!/usr/bin/perl -w
  3659. ###############################################################################
  3660. #
  3661. # Example of how to use the Spreadsheet::WriteExcel merge_range() workbook
  3662. # method with complex formatting.
  3663. #
  3664. # reverse('©'), September 2002, John McNamara, jmcnamara@cpan.org
  3665. #
  3666. use strict;
  3667. use Spreadsheet::WriteExcel;
  3668. # Create a new workbook and add a worksheet
  3669. my $workbook = Spreadsheet::WriteExcel->new('merge4.xls');
  3670. my $worksheet = $workbook->add_worksheet();
  3671. # Increase the cell size of the merged cells to highlight the formatting.
  3672. $worksheet->set_row($_, 30) for (1..11);
  3673. $worksheet->set_column('B:D', 20);
  3674. ###############################################################################
  3675. #
  3676. # Example 1: Text centered vertically and horizontally
  3677. #
  3678. my $format1 = $workbook->add_format(
  3679. border => 6,
  3680. bold => 1,
  3681. color => 'red',
  3682. valign => 'vcenter',
  3683. align => 'center',
  3684. );
  3685. $worksheet->merge_range('B2:D3', 'Vertical and horizontal', $format1);
  3686. ###############################################################################
  3687. #
  3688. # Example 2: Text aligned to the top and left
  3689. #
  3690. my $format2 = $workbook->add_format(
  3691. border => 6,
  3692. bold => 1,
  3693. color => 'red',
  3694. valign => 'top',
  3695. align => 'left',
  3696. );
  3697. $worksheet->merge_range('B5:D6', 'Aligned to the top and left', $format2);
  3698. ###############################################################################
  3699. #
  3700. # Example 3: Text aligned to the bottom and right
  3701. #
  3702. my $format3 = $workbook->add_format(
  3703. border => 6,
  3704. bold => 1,
  3705. color => 'red',
  3706. valign => 'bottom',
  3707. align => 'right',
  3708. );
  3709. $worksheet->merge_range('B8:D9', 'Aligned to the bottom and right', $format3);
  3710. ###############################################################################
  3711. #
  3712. # Example 4: Text justified (i.e. wrapped) in the cell
  3713. #
  3714. my $format4 = $workbook->add_format(
  3715. border => 6,
  3716. bold => 1,
  3717. color => 'red',
  3718. valign => 'top',
  3719. align => 'justify',
  3720. );
  3721. $worksheet->merge_range('B11:D12', 'Justified: '.'so on and ' x18, $format4);
  3722. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/merge4.pl>
  3723. =head2 Example: merge5.pl
  3724. Example of how to use the Spreadsheet::WriteExcel merge_cells() workbook
  3725. method with complex formatting and rotation.
  3726. =begin html
  3727. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/merge5.jpg" width="640" height="420" alt="Output from merge5.pl" /></center></p>
  3728. =end html
  3729. Source code for this example:
  3730. #!/usr/bin/perl -w
  3731. ###############################################################################
  3732. #
  3733. # Example of how to use the Spreadsheet::WriteExcel merge_cells() workbook
  3734. # method with complex formatting and rotation.
  3735. #
  3736. #
  3737. # reverse('©'), September 2002, John McNamara, jmcnamara@cpan.org
  3738. #
  3739. use strict;
  3740. use Spreadsheet::WriteExcel;
  3741. # Create a new workbook and add a worksheet
  3742. my $workbook = Spreadsheet::WriteExcel->new('merge5.xls');
  3743. my $worksheet = $workbook->add_worksheet();
  3744. # Increase the cell size of the merged cells to highlight the formatting.
  3745. $worksheet->set_row($_, 36) for (3..8);
  3746. $worksheet->set_column($_, $_ , 15) for (1,3,5);
  3747. ###############################################################################
  3748. #
  3749. # Rotation 1, letters run from top to bottom
  3750. #
  3751. my $format1 = $workbook->add_format(
  3752. border => 6,
  3753. bold => 1,
  3754. color => 'red',
  3755. valign => 'vcentre',
  3756. align => 'centre',
  3757. rotation => 270,
  3758. );
  3759. $worksheet->merge_range('B4:B9', 'Rotation 270', $format1);
  3760. ###############################################################################
  3761. #
  3762. # Rotation 2, 90° anticlockwise
  3763. #
  3764. my $format2 = $workbook->add_format(
  3765. border => 6,
  3766. bold => 1,
  3767. color => 'red',
  3768. valign => 'vcentre',
  3769. align => 'centre',
  3770. rotation => 90,
  3771. );
  3772. $worksheet->merge_range('D4:D9', 'Rotation 90°', $format2);
  3773. ###############################################################################
  3774. #
  3775. # Rotation 3, 90° clockwise
  3776. #
  3777. my $format3 = $workbook->add_format(
  3778. border => 6,
  3779. bold => 1,
  3780. color => 'red',
  3781. valign => 'vcentre',
  3782. align => 'centre',
  3783. rotation => -90,
  3784. );
  3785. $worksheet->merge_range('F4:F9', 'Rotation -90°', $format3);
  3786. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/merge5.pl>
  3787. =head2 Example: merge6.pl
  3788. Example of how to use the Spreadsheet::WriteExcel merge_cells() workbook
  3789. method with Unicode strings.
  3790. =begin html
  3791. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/merge6.jpg" width="640" height="420" alt="Output from merge6.pl" /></center></p>
  3792. =end html
  3793. Source code for this example:
  3794. #!/usr/bin/perl -w
  3795. ###############################################################################
  3796. #
  3797. # Example of how to use the Spreadsheet::WriteExcel merge_cells() workbook
  3798. # method with Unicode strings.
  3799. #
  3800. #
  3801. # reverse('©'), December 2005, John McNamara, jmcnamara@cpan.org
  3802. #
  3803. use strict;
  3804. use Spreadsheet::WriteExcel;
  3805. # Create a new workbook and add a worksheet
  3806. my $workbook = Spreadsheet::WriteExcel->new('merge6.xls');
  3807. my $worksheet = $workbook->add_worksheet();
  3808. # Increase the cell size of the merged cells to highlight the formatting.
  3809. $worksheet->set_row($_, 36) for 2..9;
  3810. $worksheet->set_column('B:D', 25);
  3811. # Format for the merged cells.
  3812. my $format = $workbook->add_format(
  3813. border => 6,
  3814. bold => 1,
  3815. color => 'red',
  3816. size => 20,
  3817. valign => 'vcentre',
  3818. align => 'left',
  3819. indent => 1,
  3820. );
  3821. ###############################################################################
  3822. #
  3823. # Write an Ascii string.
  3824. #
  3825. $worksheet->merge_range('B3:D4', 'ASCII: A simple string', $format);
  3826. ###############################################################################
  3827. #
  3828. # Write a UTF-16 Unicode string.
  3829. #
  3830. # A phrase in Cyrillic encoded as UTF-16BE.
  3831. my $utf16_str = pack "H*", '005500540046002d00310036003a0020'.
  3832. '042d0442043e002004440440043004370430002004'.
  3833. '3d043000200440044304410441043a043e043c0021';
  3834. # Note the extra parameter at the end to indicate UTF-16 encoding.
  3835. $worksheet->merge_range('B6:D7', $utf16_str, $format, 1);
  3836. ###############################################################################
  3837. #
  3838. # Write a UTF-8 Unicode string.
  3839. #
  3840. if ($] >= 5.008) {
  3841. my $smiley = chr 0x263a;
  3842. $worksheet->merge_range('B9:D10', "UTF-8: A Unicode smiley $smiley",
  3843. $format);
  3844. }
  3845. else {
  3846. $worksheet->merge_range('B9:D10', "UTF-8: Requires Perl 5.8", $format);
  3847. }
  3848. __END__
  3849. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/merge6.pl>
  3850. =head2 Example: mod_perl1.pl
  3851. Example of how to use the Spreadsheet::WriteExcel module to send an Excel
  3852. file to a browser using mod_perl 1 and Apache
  3853. This module ties *XLS directly to Apache, and with the correct
  3854. content-disposition/types it will prompt the user to save
  3855. the file, or open it at this location.
  3856. This script is a modification of the Spreadsheet::WriteExcel cgi.pl example.
  3857. Change the name of this file to Cgi.pm.
  3858. Change the package location to where ever you locate this package.
  3859. In the example below it is located in the WriteExcel directory.
  3860. Your httpd.conf entry for this module, should you choose to use it
  3861. as a stand alone app, should look similar to the following:
  3862. <Location /spreadsheet-test>
  3863. SetHandler perl-script
  3864. PerlHandler Spreadsheet::WriteExcel::Cgi
  3865. PerlSendHeader On
  3866. </Location>
  3867. The PerlHandler name above and the package name below *have* to match.
  3868. ###############################################################################
  3869. #
  3870. # Example of how to use the Spreadsheet::WriteExcel module to send an Excel
  3871. # file to a browser using mod_perl 1 and Apache
  3872. #
  3873. # This module ties *XLS directly to Apache, and with the correct
  3874. # content-disposition/types it will prompt the user to save
  3875. # the file, or open it at this location.
  3876. #
  3877. # This script is a modification of the Spreadsheet::WriteExcel cgi.pl example.
  3878. #
  3879. # Change the name of this file to Cgi.pm.
  3880. # Change the package location to where ever you locate this package.
  3881. # In the example below it is located in the WriteExcel directory.
  3882. #
  3883. # Your httpd.conf entry for this module, should you choose to use it
  3884. # as a stand alone app, should look similar to the following:
  3885. #
  3886. # <Location /spreadsheet-test>
  3887. # SetHandler perl-script
  3888. # PerlHandler Spreadsheet::WriteExcel::Cgi
  3889. # PerlSendHeader On
  3890. # </Location>
  3891. #
  3892. # The PerlHandler name above and the package name below *have* to match.
  3893. # Apr 2001, Thomas Sullivan, webmaster@860.org
  3894. # Feb 2001, John McNamara, jmcnamara@cpan.org
  3895. package Spreadsheet::WriteExcel::Cgi;
  3896. ##########################################
  3897. # Pragma Definitions
  3898. ##########################################
  3899. use strict;
  3900. ##########################################
  3901. # Required Modules
  3902. ##########################################
  3903. use Apache::Constants qw(:common);
  3904. use Apache::Request;
  3905. use Apache::URI; # This may not be needed
  3906. use Spreadsheet::WriteExcel;
  3907. ##########################################
  3908. # Main App Body
  3909. ##########################################
  3910. sub handler {
  3911. # New apache object
  3912. # Should you decide to use it.
  3913. my $r = Apache::Request->new(shift);
  3914. # Set the filename and send the content type
  3915. # This will appear when they save the spreadsheet
  3916. my $filename ="cgitest.xls";
  3917. ####################################################
  3918. ## Send the content type headers
  3919. ####################################################
  3920. print "Content-disposition: attachment;filename=$filename\n";
  3921. print "Content-type: application/vnd.ms-excel\n\n";
  3922. ####################################################
  3923. # Tie a filehandle to Apache's STDOUT.
  3924. # Create a new workbook and add a worksheet.
  3925. ####################################################
  3926. tie *XLS => 'Apache';
  3927. binmode(*XLS);
  3928. my $workbook = Spreadsheet::WriteExcel->new(\*XLS);
  3929. my $worksheet = $workbook->add_worksheet();
  3930. # Set the column width for column 1
  3931. $worksheet->set_column(0, 0, 20);
  3932. # Create a format
  3933. my $format = $workbook->add_format();
  3934. $format->set_bold();
  3935. $format->set_size(15);
  3936. $format->set_color('blue');
  3937. # Write to the workbook
  3938. $worksheet->write(0, 0, "Hi Excel!", $format);
  3939. # You must close the workbook for Content-disposition
  3940. $workbook->close();
  3941. }
  3942. 1;
  3943. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/mod_perl1.pl>
  3944. =head2 Example: mod_perl2.pl
  3945. Example of how to use the Spreadsheet::WriteExcel module to send an Excel
  3946. file to a browser using mod_perl 2 and Apache.
  3947. This module ties *XLS directly to Apache, and with the correct
  3948. content-disposition/types it will prompt the user to save
  3949. the file, or open it at this location.
  3950. This script is a modification of the Spreadsheet::WriteExcel cgi.pl example.
  3951. Change the name of this file to MP2Test.pm.
  3952. Change the package location to where ever you locate this package.
  3953. In the example below it is located in the WriteExcel directory.
  3954. Your httpd.conf entry for this module, should you choose to use it
  3955. as a stand alone app, should look similar to the following:
  3956. PerlModule Apache2::RequestRec
  3957. PerlModule APR::Table
  3958. PerlModule Apache2::RequestIO
  3959. <Location /spreadsheet-test>
  3960. SetHandler perl-script
  3961. PerlResponseHandler Spreadsheet::WriteExcel::MP2Test
  3962. </Location>
  3963. The PerlResponseHandler must match the package name below.
  3964. ###############################################################################
  3965. #
  3966. # Example of how to use the Spreadsheet::WriteExcel module to send an Excel
  3967. # file to a browser using mod_perl 2 and Apache.
  3968. #
  3969. # This module ties *XLS directly to Apache, and with the correct
  3970. # content-disposition/types it will prompt the user to save
  3971. # the file, or open it at this location.
  3972. #
  3973. # This script is a modification of the Spreadsheet::WriteExcel cgi.pl example.
  3974. #
  3975. # Change the name of this file to MP2Test.pm.
  3976. # Change the package location to where ever you locate this package.
  3977. # In the example below it is located in the WriteExcel directory.
  3978. #
  3979. # Your httpd.conf entry for this module, should you choose to use it
  3980. # as a stand alone app, should look similar to the following:
  3981. #
  3982. # PerlModule Apache2::RequestRec
  3983. # PerlModule APR::Table
  3984. # PerlModule Apache2::RequestIO
  3985. #
  3986. # <Location /spreadsheet-test>
  3987. # SetHandler perl-script
  3988. # PerlResponseHandler Spreadsheet::WriteExcel::MP2Test
  3989. # </Location>
  3990. #
  3991. # The PerlResponseHandler must match the package name below.
  3992. # Jun 2004, Matisse Enzer, matisse@matisse.net (mod_perl 2 version)
  3993. # Apr 2001, Thomas Sullivan, webmaster@860.org
  3994. # Feb 2001, John McNamara, jmcnamara@cpan.org
  3995. package Spreadsheet::WriteExcel::MP2Test;
  3996. ##########################################
  3997. # Pragma Definitions
  3998. ##########################################
  3999. use strict;
  4000. ##########################################
  4001. # Required Modules
  4002. ##########################################
  4003. use Apache2::Const -compile => qw( :common );
  4004. use Spreadsheet::WriteExcel;
  4005. ##########################################
  4006. # Main App Body
  4007. ##########################################
  4008. sub handler {
  4009. my($r) = @_; # Apache request object is passed to handler in mod_perl 2
  4010. # Set the filename and send the content type
  4011. # This will appear when they save the spreadsheet
  4012. my $filename ="mod_perl2_test.xls";
  4013. ####################################################
  4014. ## Send the content type headers the mod_perl 2 way
  4015. ####################################################
  4016. $r->headers_out->{'Content-Disposition'} = "attachment;filename=$filename";
  4017. $r->content_type('application/vnd.ms-excel');
  4018. ####################################################
  4019. # Tie a filehandle to Apache's STDOUT.
  4020. # Create a new workbook and add a worksheet.
  4021. ####################################################
  4022. tie *XLS => $r; # The mod_perl 2 way. Tie to the Apache::RequestRec object
  4023. binmode(*XLS);
  4024. my $workbook = Spreadsheet::WriteExcel->new(\*XLS);
  4025. my $worksheet = $workbook->add_worksheet();
  4026. # Set the column width for column 1
  4027. $worksheet->set_column(0, 0, 20);
  4028. # Create a format
  4029. my $format = $workbook->add_format();
  4030. $format->set_bold();
  4031. $format->set_size(15);
  4032. $format->set_color('blue');
  4033. # Write to the workbook
  4034. $worksheet->write(0, 0, 'Hi Excel! from ' . $r->hostname , $format);
  4035. # You must close the workbook for Content-disposition
  4036. $workbook->close();
  4037. return Apache2::Const::OK;
  4038. }
  4039. 1;
  4040. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/mod_perl2.pl>
  4041. =head2 Example: outline.pl
  4042. Example of how use Spreadsheet::WriteExcel to generate Excel outlines and
  4043. grouping.
  4044. Excel allows you to group rows or columns so that they can be hidden or
  4045. displayed with a single mouse click. This feature is referred to as outlines.
  4046. Outlines can reduce complex data down to a few salient sub-totals or
  4047. summaries.
  4048. This feature is best viewed in Excel but the following is an ASCII
  4049. representation of what a worksheet with three outlines might look like.
  4050. Rows 3-4 and rows 7-8 are grouped at level 2. Rows 2-9 are grouped at
  4051. level 1. The lines at the left hand side are called outline level bars.
  4052. ------------------------------------------
  4053. 1 2 3 | | A | B | C | D | ...
  4054. ------------------------------------------
  4055. _ | 1 | A | | | | ...
  4056. | _ | 2 | B | | | | ...
  4057. | | | 3 | (C) | | | | ...
  4058. | | | 4 | (D) | | | | ...
  4059. | - | 5 | E | | | | ...
  4060. | _ | 6 | F | | | | ...
  4061. | | | 7 | (G) | | | | ...
  4062. | | | 8 | (H) | | | | ...
  4063. | - | 9 | I | | | | ...
  4064. - | . | ... | ... | ... | ... | ...
  4065. Clicking the minus sign on each of the level 2 outlines will collapse and
  4066. hide the data as shown in the next figure. The minus sign changes to a plus
  4067. sign to indicate that the data in the outline is hidden.
  4068. ------------------------------------------
  4069. 1 2 3 | | A | B | C | D | ...
  4070. ------------------------------------------
  4071. _ | 1 | A | | | | ...
  4072. | | 2 | B | | | | ...
  4073. | + | 5 | E | | | | ...
  4074. | | 6 | F | | | | ...
  4075. | + | 9 | I | | | | ...
  4076. - | . | ... | ... | ... | ... | ...
  4077. Clicking on the minus sign on the level 1 outline will collapse the remaining
  4078. rows as follows:
  4079. ------------------------------------------
  4080. 1 2 3 | | A | B | C | D | ...
  4081. ------------------------------------------
  4082. | 1 | A | | | | ...
  4083. + | . | ... | ... | ... | ... | ...
  4084. See the main Spreadsheet::WriteExcel documentation for more information.
  4085. =begin html
  4086. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/outline.jpg" width="640" height="420" alt="Output from outline.pl" /></center></p>
  4087. =end html
  4088. Source code for this example:
  4089. #!/usr/bin/perl -w
  4090. ###############################################################################
  4091. #
  4092. # Example of how use Spreadsheet::WriteExcel to generate Excel outlines and
  4093. # grouping.
  4094. #
  4095. #
  4096. # Excel allows you to group rows or columns so that they can be hidden or
  4097. # displayed with a single mouse click. This feature is referred to as outlines.
  4098. #
  4099. # Outlines can reduce complex data down to a few salient sub-totals or
  4100. # summaries.
  4101. #
  4102. # This feature is best viewed in Excel but the following is an ASCII
  4103. # representation of what a worksheet with three outlines might look like.
  4104. # Rows 3-4 and rows 7-8 are grouped at level 2. Rows 2-9 are grouped at
  4105. # level 1. The lines at the left hand side are called outline level bars.
  4106. #
  4107. #
  4108. # ------------------------------------------
  4109. # 1 2 3 | | A | B | C | D | ...
  4110. # ------------------------------------------
  4111. # _ | 1 | A | | | | ...
  4112. # | _ | 2 | B | | | | ...
  4113. # | | | 3 | (C) | | | | ...
  4114. # | | | 4 | (D) | | | | ...
  4115. # | - | 5 | E | | | | ...
  4116. # | _ | 6 | F | | | | ...
  4117. # | | | 7 | (G) | | | | ...
  4118. # | | | 8 | (H) | | | | ...
  4119. # | - | 9 | I | | | | ...
  4120. # - | . | ... | ... | ... | ... | ...
  4121. #
  4122. #
  4123. # Clicking the minus sign on each of the level 2 outlines will collapse and
  4124. # hide the data as shown in the next figure. The minus sign changes to a plus
  4125. # sign to indicate that the data in the outline is hidden.
  4126. #
  4127. # ------------------------------------------
  4128. # 1 2 3 | | A | B | C | D | ...
  4129. # ------------------------------------------
  4130. # _ | 1 | A | | | | ...
  4131. # | | 2 | B | | | | ...
  4132. # | + | 5 | E | | | | ...
  4133. # | | 6 | F | | | | ...
  4134. # | + | 9 | I | | | | ...
  4135. # - | . | ... | ... | ... | ... | ...
  4136. #
  4137. #
  4138. # Clicking on the minus sign on the level 1 outline will collapse the remaining
  4139. # rows as follows:
  4140. #
  4141. # ------------------------------------------
  4142. # 1 2 3 | | A | B | C | D | ...
  4143. # ------------------------------------------
  4144. # | 1 | A | | | | ...
  4145. # + | . | ... | ... | ... | ... | ...
  4146. #
  4147. # See the main Spreadsheet::WriteExcel documentation for more information.
  4148. #
  4149. # reverse('©'), April 2003, John McNamara, jmcnamara@cpan.org
  4150. #
  4151. use strict;
  4152. use Spreadsheet::WriteExcel;
  4153. # Create a new workbook and add some worksheets
  4154. my $workbook = Spreadsheet::WriteExcel->new('outline.xls');
  4155. my $worksheet1 = $workbook->add_worksheet('Outlined Rows');
  4156. my $worksheet2 = $workbook->add_worksheet('Collapsed Rows');
  4157. my $worksheet3 = $workbook->add_worksheet('Outline Columns');
  4158. my $worksheet4 = $workbook->add_worksheet('Outline levels');
  4159. # Add a general format
  4160. my $bold = $workbook->add_format(bold => 1);
  4161. ###############################################################################
  4162. #
  4163. # Example 1: Create a worksheet with outlined rows. It also includes SUBTOTAL()
  4164. # functions so that it looks like the type of automatic outlines that are
  4165. # generated when you use the Excel Data->SubTotals menu item.
  4166. #
  4167. # For outlines the important parameters are $hidden and $level. Rows with the
  4168. # same $level are grouped together. The group will be collapsed if $hidden is
  4169. # non-zero. $height and $XF are assigned default values if they are undef.
  4170. #
  4171. # The syntax is: set_row($row, $height, $XF, $hidden, $level, $collapsed)
  4172. #
  4173. $worksheet1->set_row(1, undef, undef, 0, 2);
  4174. $worksheet1->set_row(2, undef, undef, 0, 2);
  4175. $worksheet1->set_row(3, undef, undef, 0, 2);
  4176. $worksheet1->set_row(4, undef, undef, 0, 2);
  4177. $worksheet1->set_row(5, undef, undef, 0, 1);
  4178. $worksheet1->set_row(6, undef, undef, 0, 2);
  4179. $worksheet1->set_row(7, undef, undef, 0, 2);
  4180. $worksheet1->set_row(8, undef, undef, 0, 2);
  4181. $worksheet1->set_row(9, undef, undef, 0, 2);
  4182. $worksheet1->set_row(10, undef, undef, 0, 1);
  4183. # Add a column format for clarity
  4184. $worksheet1->set_column('A:A', 20);
  4185. # Add the data, labels and formulas
  4186. $worksheet1->write('A1', 'Region', $bold);
  4187. $worksheet1->write('A2', 'North');
  4188. $worksheet1->write('A3', 'North');
  4189. $worksheet1->write('A4', 'North');
  4190. $worksheet1->write('A5', 'North');
  4191. $worksheet1->write('A6', 'North Total', $bold);
  4192. $worksheet1->write('B1', 'Sales', $bold);
  4193. $worksheet1->write('B2', 1000);
  4194. $worksheet1->write('B3', 1200);
  4195. $worksheet1->write('B4', 900);
  4196. $worksheet1->write('B5', 1200);
  4197. $worksheet1->write('B6', '=SUBTOTAL(9,B2:B5)', $bold);
  4198. $worksheet1->write('A7', 'South');
  4199. $worksheet1->write('A8', 'South');
  4200. $worksheet1->write('A9', 'South');
  4201. $worksheet1->write('A10', 'South');
  4202. $worksheet1->write('A11', 'South Total', $bold);
  4203. $worksheet1->write('B7', 400);
  4204. $worksheet1->write('B8', 600);
  4205. $worksheet1->write('B9', 500);
  4206. $worksheet1->write('B10', 600);
  4207. $worksheet1->write('B11', '=SUBTOTAL(9,B7:B10)', $bold);
  4208. $worksheet1->write('A12', 'Grand Total', $bold);
  4209. $worksheet1->write('B12', '=SUBTOTAL(9,B2:B10)', $bold);
  4210. ###############################################################################
  4211. #
  4212. # Example 2: Create a worksheet with outlined rows. This is the same as the
  4213. # previous example except that the rows are collapsed.
  4214. # Note: We need to indicate the row that contains the collapsed symbol '+'
  4215. # with the optional parameter, $collapsed.
  4216. # The group will be collapsed if $hidden is non-zero.
  4217. # The syntax is: set_row($row, $height, $XF, $hidden, $level, $collapsed)
  4218. #
  4219. $worksheet2->set_row(1, undef, undef, 1, 2);
  4220. $worksheet2->set_row(2, undef, undef, 1, 2);
  4221. $worksheet2->set_row(3, undef, undef, 1, 2);
  4222. $worksheet2->set_row(4, undef, undef, 1, 2);
  4223. $worksheet2->set_row(5, undef, undef, 1, 1);
  4224. $worksheet2->set_row(6, undef, undef, 1, 2);
  4225. $worksheet2->set_row(7, undef, undef, 1, 2);
  4226. $worksheet2->set_row(8, undef, undef, 1, 2);
  4227. $worksheet2->set_row(9, undef, undef, 1, 2);
  4228. $worksheet2->set_row(10, undef, undef, 1, 1);
  4229. $worksheet2->set_row(11, undef, undef, 0, 0, 1);
  4230. # Add a column format for clarity
  4231. $worksheet2->set_column('A:A', 20);
  4232. # Add the data, labels and formulas
  4233. $worksheet2->write('A1', 'Region', $bold);
  4234. $worksheet2->write('A2', 'North');
  4235. $worksheet2->write('A3', 'North');
  4236. $worksheet2->write('A4', 'North');
  4237. $worksheet2->write('A5', 'North');
  4238. $worksheet2->write('A6', 'North Total', $bold);
  4239. $worksheet2->write('B1', 'Sales', $bold);
  4240. $worksheet2->write('B2', 1000);
  4241. $worksheet2->write('B3', 1200);
  4242. $worksheet2->write('B4', 900);
  4243. $worksheet2->write('B5', 1200);
  4244. $worksheet2->write('B6', '=SUBTOTAL(9,B2:B5)', $bold);
  4245. $worksheet2->write('A7', 'South');
  4246. $worksheet2->write('A8', 'South');
  4247. $worksheet2->write('A9', 'South');
  4248. $worksheet2->write('A10', 'South');
  4249. $worksheet2->write('A11', 'South Total', $bold);
  4250. $worksheet2->write('B7', 400);
  4251. $worksheet2->write('B8', 600);
  4252. $worksheet2->write('B9', 500);
  4253. $worksheet2->write('B10', 600);
  4254. $worksheet2->write('B11', '=SUBTOTAL(9,B7:B10)', $bold);
  4255. $worksheet2->write('A12', 'Grand Total', $bold);
  4256. $worksheet2->write('B12', '=SUBTOTAL(9,B2:B10)', $bold);
  4257. ###############################################################################
  4258. #
  4259. # Example 3: Create a worksheet with outlined columns.
  4260. #
  4261. my $data = [
  4262. ['Month', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',' Total'],
  4263. ['North', 50, 20, 15, 25, 65, 80, ,'=SUM(B2:G2)'],
  4264. ['South', 10, 20, 30, 50, 50, 50, ,'=SUM(B3:G3)'],
  4265. ['East', 45, 75, 50, 15, 75, 100, ,'=SUM(B4:G4)'],
  4266. ['West', 15, 15, 55, 35, 20, 50, ,'=SUM(B5:G6)'],
  4267. ];
  4268. # Add bold format to the first row
  4269. $worksheet3->set_row(0, undef, $bold);
  4270. # Syntax: set_column($col1, $col2, $width, $XF, $hidden, $level, $collapsed)
  4271. $worksheet3->set_column('A:A', 10, $bold );
  4272. $worksheet3->set_column('B:G', 5, undef, 0, 1);
  4273. $worksheet3->set_column('H:H', 10);
  4274. # Write the data and a formula
  4275. $worksheet3->write_col('A1', $data);
  4276. $worksheet3->write('H6', '=SUM(H2:H5)', $bold);
  4277. ###############################################################################
  4278. #
  4279. # Example 4: Show all possible outline levels.
  4280. #
  4281. my $levels = ["Level 1", "Level 2", "Level 3", "Level 4",
  4282. "Level 5", "Level 6", "Level 7", "Level 6",
  4283. "Level 5", "Level 4", "Level 3", "Level 2", "Level 1"];
  4284. $worksheet4->write_col('A1', $levels);
  4285. $worksheet4->set_row(0, undef, undef, undef, 1);
  4286. $worksheet4->set_row(1, undef, undef, undef, 2);
  4287. $worksheet4->set_row(2, undef, undef, undef, 3);
  4288. $worksheet4->set_row(3, undef, undef, undef, 4);
  4289. $worksheet4->set_row(4, undef, undef, undef, 5);
  4290. $worksheet4->set_row(5, undef, undef, undef, 6);
  4291. $worksheet4->set_row(6, undef, undef, undef, 7);
  4292. $worksheet4->set_row(7, undef, undef, undef, 6);
  4293. $worksheet4->set_row(8, undef, undef, undef, 5);
  4294. $worksheet4->set_row(9, undef, undef, undef, 4);
  4295. $worksheet4->set_row(10, undef, undef, undef, 3);
  4296. $worksheet4->set_row(11, undef, undef, undef, 2);
  4297. $worksheet4->set_row(12, undef, undef, undef, 1);
  4298. __END__
  4299. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/outline.pl>
  4300. =head2 Example: outline_collapsed.pl
  4301. Example of how use Spreadsheet::WriteExcel to generate Excel outlines and
  4302. grouping.
  4303. These example focus mainly on collapsed outlines. See also the
  4304. outlines.pl example program for more general examples.
  4305. =begin html
  4306. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/outline_collapsed.jpg" width="640" height="420" alt="Output from outline_collapsed.pl" /></center></p>
  4307. =end html
  4308. Source code for this example:
  4309. #!/usr/bin/perl -w
  4310. ###############################################################################
  4311. #
  4312. # Example of how use Spreadsheet::WriteExcel to generate Excel outlines and
  4313. # grouping.
  4314. #
  4315. # These example focus mainly on collapsed outlines. See also the
  4316. # outlines.pl example program for more general examples.
  4317. #
  4318. # reverse('©'), March 2008, John McNamara, jmcnamara@cpan.org
  4319. #
  4320. use strict;
  4321. use Spreadsheet::WriteExcel;
  4322. # Create a new workbook and add some worksheets
  4323. my $workbook = Spreadsheet::WriteExcel->new('outline_collapsed.xls');
  4324. my $worksheet1 = $workbook->add_worksheet('Outlined Rows');
  4325. my $worksheet2 = $workbook->add_worksheet('Collapsed Rows 1');
  4326. my $worksheet3 = $workbook->add_worksheet('Collapsed Rows 2');
  4327. my $worksheet4 = $workbook->add_worksheet('Collapsed Rows 3');
  4328. my $worksheet5 = $workbook->add_worksheet('Outline Columns');
  4329. my $worksheet6 = $workbook->add_worksheet('Collapsed Columns');
  4330. # Add a general format
  4331. my $bold = $workbook->add_format(bold => 1);
  4332. #
  4333. # This function will generate the same data and sub-totals on each worksheet.
  4334. #
  4335. sub create_sub_totals {
  4336. my $worksheet = $_[0];
  4337. # Add a column format for clarity
  4338. $worksheet->set_column('A:A', 20);
  4339. # Add the data, labels and formulas
  4340. $worksheet->write('A1', 'Region', $bold);
  4341. $worksheet->write('A2', 'North');
  4342. $worksheet->write('A3', 'North');
  4343. $worksheet->write('A4', 'North');
  4344. $worksheet->write('A5', 'North');
  4345. $worksheet->write('A6', 'North Total', $bold);
  4346. $worksheet->write('B1', 'Sales', $bold);
  4347. $worksheet->write('B2', 1000);
  4348. $worksheet->write('B3', 1200);
  4349. $worksheet->write('B4', 900);
  4350. $worksheet->write('B5', 1200);
  4351. $worksheet->write('B6', '=SUBTOTAL(9,B2:B5)', $bold);
  4352. $worksheet->write('A7', 'South');
  4353. $worksheet->write('A8', 'South');
  4354. $worksheet->write('A9', 'South');
  4355. $worksheet->write('A10', 'South');
  4356. $worksheet->write('A11', 'South Total', $bold);
  4357. $worksheet->write('B7', 400);
  4358. $worksheet->write('B8', 600);
  4359. $worksheet->write('B9', 500);
  4360. $worksheet->write('B10', 600);
  4361. $worksheet->write('B11', '=SUBTOTAL(9,B7:B10)', $bold);
  4362. $worksheet->write('A12', 'Grand Total', $bold);
  4363. $worksheet->write('B12', '=SUBTOTAL(9,B2:B10)', $bold);
  4364. }
  4365. ###############################################################################
  4366. #
  4367. # Example 1: Create a worksheet with outlined rows. It also includes SUBTOTAL()
  4368. # functions so that it looks like the type of automatic outlines that are
  4369. # generated when you use the Excel Data->SubTotals menu item.
  4370. #
  4371. # The syntax is: set_row($row, $height, $XF, $hidden, $level, $collapsed)
  4372. $worksheet1->set_row(1, undef, undef, 0, 2);
  4373. $worksheet1->set_row(2, undef, undef, 0, 2);
  4374. $worksheet1->set_row(3, undef, undef, 0, 2);
  4375. $worksheet1->set_row(4, undef, undef, 0, 2);
  4376. $worksheet1->set_row(5, undef, undef, 0, 1);
  4377. $worksheet1->set_row(6, undef, undef, 0, 2);
  4378. $worksheet1->set_row(7, undef, undef, 0, 2);
  4379. $worksheet1->set_row(8, undef, undef, 0, 2);
  4380. $worksheet1->set_row(9, undef, undef, 0, 2);
  4381. $worksheet1->set_row(10, undef, undef, 0, 1);
  4382. # Write the sub-total data that is common to the row examples.
  4383. create_sub_totals($worksheet1);
  4384. ###############################################################################
  4385. #
  4386. # Example 2: Create a worksheet with collapsed outlined rows.
  4387. # This is the same as the example 1 except that the all rows are collapsed.
  4388. # Note: We need to indicate the row that contains the collapsed symbol '+' with
  4389. # the optional parameter, $collapsed.
  4390. $worksheet2->set_row(1, undef, undef, 1, 2);
  4391. $worksheet2->set_row(2, undef, undef, 1, 2);
  4392. $worksheet2->set_row(3, undef, undef, 1, 2);
  4393. $worksheet2->set_row(4, undef, undef, 1, 2);
  4394. $worksheet2->set_row(5, undef, undef, 1, 1);
  4395. $worksheet2->set_row(6, undef, undef, 1, 2);
  4396. $worksheet2->set_row(7, undef, undef, 1, 2);
  4397. $worksheet2->set_row(8, undef, undef, 1, 2);
  4398. $worksheet2->set_row(9, undef, undef, 1, 2);
  4399. $worksheet2->set_row(10, undef, undef, 1, 1);
  4400. $worksheet2->set_row(11, undef, undef, 0, 0, 1);
  4401. # Write the sub-total data that is common to the row examples.
  4402. create_sub_totals($worksheet2);
  4403. ###############################################################################
  4404. #
  4405. # Example 3: Create a worksheet with collapsed outlined rows.
  4406. # Same as the example 1 except that the two sub-totals are collapsed.
  4407. $worksheet3->set_row(1, undef, undef, 1, 2);
  4408. $worksheet3->set_row(2, undef, undef, 1, 2);
  4409. $worksheet3->set_row(3, undef, undef, 1, 2);
  4410. $worksheet3->set_row(4, undef, undef, 1, 2);
  4411. $worksheet3->set_row(5, undef, undef, 0, 1, 1);
  4412. $worksheet3->set_row(6, undef, undef, 1, 2);
  4413. $worksheet3->set_row(7, undef, undef, 1, 2);
  4414. $worksheet3->set_row(8, undef, undef, 1, 2);
  4415. $worksheet3->set_row(9, undef, undef, 1, 2);
  4416. $worksheet3->set_row(10, undef, undef, 0, 1, 1);
  4417. # Write the sub-total data that is common to the row examples.
  4418. create_sub_totals($worksheet3);
  4419. ###############################################################################
  4420. #
  4421. # Example 4: Create a worksheet with outlined rows.
  4422. # Same as the example 1 except that the two sub-totals are collapsed.
  4423. $worksheet4->set_row(1, undef, undef, 1, 2);
  4424. $worksheet4->set_row(2, undef, undef, 1, 2);
  4425. $worksheet4->set_row(3, undef, undef, 1, 2);
  4426. $worksheet4->set_row(4, undef, undef, 1, 2);
  4427. $worksheet4->set_row(5, undef, undef, 1, 1, 1);
  4428. $worksheet4->set_row(6, undef, undef, 1, 2);
  4429. $worksheet4->set_row(7, undef, undef, 1, 2);
  4430. $worksheet4->set_row(8, undef, undef, 1, 2);
  4431. $worksheet4->set_row(9, undef, undef, 1, 2);
  4432. $worksheet4->set_row(10, undef, undef, 1, 1, 1);
  4433. $worksheet4->set_row(11, undef, undef, 0, 0, 1);
  4434. # Write the sub-total data that is common to the row examples.
  4435. create_sub_totals($worksheet4);
  4436. ###############################################################################
  4437. #
  4438. # Example 5: Create a worksheet with outlined columns.
  4439. #
  4440. my $data = [
  4441. ['Month', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',' Total'],
  4442. ['North', 50, 20, 15, 25, 65, 80, ,'=SUM(B2:G2)'],
  4443. ['South', 10, 20, 30, 50, 50, 50, ,'=SUM(B3:G3)'],
  4444. ['East', 45, 75, 50, 15, 75, 100, ,'=SUM(B4:G4)'],
  4445. ['West', 15, 15, 55, 35, 20, 50, ,'=SUM(B5:G6)'],
  4446. ];
  4447. # Add bold format to the first row
  4448. $worksheet5->set_row(0, undef, $bold);
  4449. # Syntax: set_column($col1, $col2, $width, $XF, $hidden, $level, $collapsed)
  4450. $worksheet5->set_column('A:A', 10, $bold );
  4451. $worksheet5->set_column('B:G', 5, undef, 0, 1);
  4452. $worksheet5->set_column('H:H', 10 );
  4453. # Write the data and a formula
  4454. $worksheet5->write_col('A1', $data);
  4455. $worksheet5->write('H6', '=SUM(H2:H5)', $bold);
  4456. ###############################################################################
  4457. #
  4458. # Example 6: Create a worksheet with collapsed outlined columns.
  4459. # This is the same as the previous example except collapsed columns.
  4460. # Add bold format to the first row
  4461. $worksheet6->set_row(0, undef, $bold);
  4462. # Syntax: set_column($col1, $col2, $width, $XF, $hidden, $level, $collapsed)
  4463. $worksheet6->set_column('A:A', 10, $bold );
  4464. $worksheet6->set_column('B:G', 5, undef, 1, 1 );
  4465. $worksheet6->set_column('H:H', 10, undef, 0, 0, 1);
  4466. # Write the data and a formula
  4467. $worksheet6->write_col('A1', $data);
  4468. $worksheet6->write('H6', '=SUM(H2:H5)', $bold);
  4469. __END__
  4470. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/outline_collapsed.pl>
  4471. =head2 Example: panes.pl
  4472. Example of using the WriteExcel module to create worksheet panes.
  4473. =begin html
  4474. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/panes.jpg" width="640" height="420" alt="Output from panes.pl" /></center></p>
  4475. =end html
  4476. Source code for this example:
  4477. #!/usr/bin/perl -w
  4478. #######################################################################
  4479. #
  4480. # Example of using the WriteExcel module to create worksheet panes.
  4481. #
  4482. # reverse('©'), May 2001, John McNamara, jmcnamara@cpan.org
  4483. #
  4484. use strict;
  4485. use Spreadsheet::WriteExcel;
  4486. my $workbook = Spreadsheet::WriteExcel->new("panes.xls");
  4487. my $worksheet1 = $workbook->add_worksheet('Panes 1');
  4488. my $worksheet2 = $workbook->add_worksheet('Panes 2');
  4489. my $worksheet3 = $workbook->add_worksheet('Panes 3');
  4490. my $worksheet4 = $workbook->add_worksheet('Panes 4');
  4491. # Freeze panes
  4492. $worksheet1->freeze_panes(1, 0); # 1 row
  4493. $worksheet2->freeze_panes(0, 1); # 1 column
  4494. $worksheet3->freeze_panes(1, 1); # 1 row and column
  4495. # Split panes.
  4496. # The divisions must be specified in terms of row and column dimensions.
  4497. # The default row height is 12.75 and the default column width is 8.43
  4498. #
  4499. $worksheet4->split_panes(12.75, 8.43, 1, 1); # 1 row and column
  4500. #######################################################################
  4501. #
  4502. # Set up some formatting and text to highlight the panes
  4503. #
  4504. my $header = $workbook->add_format();
  4505. $header->set_color('white');
  4506. $header->set_align('center');
  4507. $header->set_align('vcenter');
  4508. $header->set_pattern();
  4509. $header->set_fg_color('green');
  4510. my $center = $workbook->add_format();
  4511. $center->set_align('center');
  4512. #######################################################################
  4513. #
  4514. # Sheet 1
  4515. #
  4516. $worksheet1->set_column('A:I', 16);
  4517. $worksheet1->set_row(0, 20);
  4518. $worksheet1->set_selection('C3');
  4519. for my $i (0..8){
  4520. $worksheet1->write(0, $i, 'Scroll down', $header);
  4521. }
  4522. for my $i (1..100){
  4523. for my $j (0..8){
  4524. $worksheet1->write($i, $j, $i+1, $center);
  4525. }
  4526. }
  4527. #######################################################################
  4528. #
  4529. # Sheet 2
  4530. #
  4531. $worksheet2->set_column('A:A', 16);
  4532. $worksheet2->set_selection('C3');
  4533. for my $i (0..49){
  4534. $worksheet2->set_row($i, 15);
  4535. $worksheet2->write($i, 0, 'Scroll right', $header);
  4536. }
  4537. for my $i (0..49){
  4538. for my $j (1..25){
  4539. $worksheet2->write($i, $j, $j, $center);
  4540. }
  4541. }
  4542. #######################################################################
  4543. #
  4544. # Sheet 3
  4545. #
  4546. $worksheet3->set_column('A:Z', 16);
  4547. $worksheet3->set_selection('C3');
  4548. for my $i (1..25){
  4549. $worksheet3->write(0, $i, 'Scroll down', $header);
  4550. }
  4551. for my $i (1..49){
  4552. $worksheet3->write($i, 0, 'Scroll right', $header);
  4553. }
  4554. for my $i (1..49){
  4555. for my $j (1..25){
  4556. $worksheet3->write($i, $j, $j, $center);
  4557. }
  4558. }
  4559. #######################################################################
  4560. #
  4561. # Sheet 4
  4562. #
  4563. $worksheet4->set_selection('C3');
  4564. for my $i (1..25){
  4565. $worksheet4->write(0, $i, 'Scroll', $center);
  4566. }
  4567. for my $i (1..49){
  4568. $worksheet4->write($i, 0, 'Scroll', $center);
  4569. }
  4570. for my $i (1..49){
  4571. for my $j (1..25){
  4572. $worksheet4->write($i, $j, $j, $center);
  4573. }
  4574. }
  4575. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/panes.pl>
  4576. =head2 Example: properties.pl
  4577. An example of adding document properties to a Spreadsheet::WriteExcel file.
  4578. =begin html
  4579. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/properties.jpg" width="640" height="420" alt="Output from properties.pl" /></center></p>
  4580. =end html
  4581. Source code for this example:
  4582. #!/usr/bin/perl -w
  4583. ##############################################################################
  4584. #
  4585. # An example of adding document properties to a Spreadsheet::WriteExcel file.
  4586. #
  4587. # reverse('©'), August 2008, John McNamara, jmcnamara@cpan.org
  4588. #
  4589. use strict;
  4590. use Spreadsheet::WriteExcel;
  4591. my $workbook = Spreadsheet::WriteExcel->new('properties.xls');
  4592. my $worksheet = $workbook->add_worksheet();
  4593. $workbook->set_properties(
  4594. title => 'This is an example spreadsheet',
  4595. subject => 'With document properties',
  4596. author => 'John McNamara',
  4597. manager => 'Dr. Heinz Doofenshmirtz ',
  4598. company => 'of Wolves',
  4599. category => 'Example spreadsheets',
  4600. keywords => 'Sample, Example, Properties',
  4601. comments => 'Created with Perl and Spreadsheet::WriteExcel',
  4602. );
  4603. $worksheet->set_column('A:A', 50);
  4604. $worksheet->write('A1', 'Select File->Properties to see the file properties');
  4605. __END__
  4606. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/properties.pl>
  4607. =head2 Example: protection.pl
  4608. Example of cell locking and formula hiding in an Excel worksheet via
  4609. the Spreadsheet::WriteExcel module.
  4610. =begin html
  4611. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/protection.jpg" width="640" height="420" alt="Output from protection.pl" /></center></p>
  4612. =end html
  4613. Source code for this example:
  4614. #!/usr/bin/perl -w
  4615. ########################################################################
  4616. #
  4617. # Example of cell locking and formula hiding in an Excel worksheet via
  4618. # the Spreadsheet::WriteExcel module.
  4619. #
  4620. # reverse('©'), August 2001, John McNamara, jmcnamara@cpan.org
  4621. #
  4622. use strict;
  4623. use Spreadsheet::WriteExcel;
  4624. my $workbook = Spreadsheet::WriteExcel->new("protection.xls");
  4625. my $worksheet = $workbook->add_worksheet();
  4626. # Create some format objects
  4627. my $locked = $workbook->add_format(locked => 1);
  4628. my $unlocked = $workbook->add_format(locked => 0);
  4629. my $hidden = $workbook->add_format(hidden => 1);
  4630. # Format the columns
  4631. $worksheet->set_column('A:A', 42);
  4632. $worksheet->set_selection('B3:B3');
  4633. # Protect the worksheet
  4634. $worksheet->protect();
  4635. # Examples of cell locking and hiding
  4636. $worksheet->write('A1', 'Cell B1 is locked. It cannot be edited.');
  4637. $worksheet->write('B1', '=1+2', $locked);
  4638. $worksheet->write('A2', 'Cell B2 is unlocked. It can be edited.');
  4639. $worksheet->write('B2', '=1+2', $unlocked);
  4640. $worksheet->write('A3', "Cell B3 is hidden. The formula isn't visible.");
  4641. $worksheet->write('B3', '=1+2', $hidden);
  4642. $worksheet->write('A5', 'Use Menu->Tools->Protection->Unprotect Sheet');
  4643. $worksheet->write('A6', 'to remove the worksheet protection. ');
  4644. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/protection.pl>
  4645. =head2 Example: repeat.pl
  4646. Example of writing repeated formulas.
  4647. =begin html
  4648. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/repeat.jpg" width="640" height="420" alt="Output from repeat.pl" /></center></p>
  4649. =end html
  4650. Source code for this example:
  4651. #!/usr/bin/perl -w
  4652. ######################################################################
  4653. #
  4654. # Example of writing repeated formulas.
  4655. #
  4656. # reverse('©'), August 2002, John McNamara, jmcnamara@cpan.org
  4657. #
  4658. use strict;
  4659. use Spreadsheet::WriteExcel;
  4660. my $workbook = Spreadsheet::WriteExcel->new("repeat.xls");
  4661. my $worksheet = $workbook->add_worksheet();
  4662. my $limit = 1000;
  4663. # Write a column of numbers
  4664. for my $row (0..$limit) {
  4665. $worksheet->write($row, 0, $row);
  4666. }
  4667. # Store a formula
  4668. my $formula = $worksheet->store_formula('=A1*5+4');
  4669. # Write a column of formulas based on the stored formula
  4670. for my $row (0..$limit) {
  4671. $worksheet->repeat_formula($row, 1, $formula, undef,
  4672. qr/^A1$/, 'A'.($row+1));
  4673. }
  4674. # Direct formula writing. As a speed comparison uncomment the
  4675. # following and run the program again
  4676. #for my $row (0..$limit) {
  4677. # $worksheet->write_formula($row, 2, '=A'.($row+1).'*5+4');
  4678. #}
  4679. __END__
  4680. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/repeat.pl>
  4681. =head2 Example: right_to_left.pl
  4682. Example of how to change the default worksheet direction from
  4683. left-to-right to right-to-left as required by some eastern verions
  4684. of Excel.
  4685. =begin html
  4686. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/right_to_left.jpg" width="640" height="420" alt="Output from right_to_left.pl" /></center></p>
  4687. =end html
  4688. Source code for this example:
  4689. #!/usr/bin/perl -w
  4690. #######################################################################
  4691. #
  4692. # Example of how to change the default worksheet direction from
  4693. # left-to-right to right-to-left as required by some eastern verions
  4694. # of Excel.
  4695. #
  4696. # reverse('©'), January 2006, John McNamara, jmcnamara@cpan.org
  4697. #
  4698. use strict;
  4699. use Spreadsheet::WriteExcel;
  4700. my $workbook = Spreadsheet::WriteExcel->new("right_to_left.xls");
  4701. my $worksheet1 = $workbook->add_worksheet();
  4702. my $worksheet2 = $workbook->add_worksheet();
  4703. $worksheet2->right_to_left();
  4704. $worksheet1->write(0, 0, 'Hello'); # A1, B1, C1, ...
  4705. $worksheet2->write(0, 0, 'Hello'); # ..., C1, B1, A1
  4706. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/right_to_left.pl>
  4707. =head2 Example: row_wrap.pl
  4708. Demonstrates how to wrap data from one worksheet onto another.
  4709. Excel has a row limit of 65536 rows. Sometimes the amount of row data to be
  4710. written to a file is greater than this limit. In this case it is a useful
  4711. technique to wrap the data from one worksheet onto the next so that we get
  4712. something like the following:
  4713. Sheet1 Row 1 - 65536
  4714. Sheet2 Row 65537 - 131072
  4715. Sheet3 Row 131073 - ...
  4716. In order to achieve this we use a single worksheet reference and
  4717. reinitialise it to point to a new worksheet when required.
  4718. =begin html
  4719. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/row_wrap.jpg" width="640" height="420" alt="Output from row_wrap.pl" /></center></p>
  4720. =end html
  4721. Source code for this example:
  4722. #!/usr/bin/perl -w
  4723. ##############################################################################
  4724. #
  4725. # Demonstrates how to wrap data from one worksheet onto another.
  4726. #
  4727. # Excel has a row limit of 65536 rows. Sometimes the amount of row data to be
  4728. # written to a file is greater than this limit. In this case it is a useful
  4729. # technique to wrap the data from one worksheet onto the next so that we get
  4730. # something like the following:
  4731. #
  4732. # Sheet1 Row 1 - 65536
  4733. # Sheet2 Row 65537 - 131072
  4734. # Sheet3 Row 131073 - ...
  4735. #
  4736. # In order to achieve this we use a single worksheet reference and
  4737. # reinitialise it to point to a new worksheet when required.
  4738. #
  4739. # reverse('©'), May 2006, John McNamara, jmcnamara@cpan.org
  4740. #
  4741. use strict;
  4742. use Spreadsheet::WriteExcel;
  4743. my $workbook = Spreadsheet::WriteExcel->new('row_wrap.xls');
  4744. my $worksheet = $workbook->add_worksheet();
  4745. # Worksheet formatting.
  4746. $worksheet->set_column('A:A', 20);
  4747. # For the sake of this example we will use a small row limit. In order to use
  4748. # the entire row range set the $row_limit to 65536.
  4749. my $row_limit = 10;
  4750. my $row = 0;
  4751. for my $count (1 .. 2 * $row_limit +10) {
  4752. # When we hit the row limit we redirect the output
  4753. # to a new worksheet and reset the row number.
  4754. if ($row == $row_limit) {
  4755. $worksheet = $workbook->add_worksheet();
  4756. $row = 0;
  4757. # Repeat any worksheet formatting.
  4758. $worksheet->set_column('A:A', 20);
  4759. }
  4760. $worksheet->write($row, 0, "This is row $count");
  4761. $row++;
  4762. }
  4763. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/row_wrap.pl>
  4764. =head2 Example: sales.pl
  4765. Example of a sales worksheet to demonstrate several different features.
  4766. Also uses functions from the L<Spreadsheet::WriteExcel::Utility> module.
  4767. =begin html
  4768. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/sales.jpg" width="640" height="420" alt="Output from sales.pl" /></center></p>
  4769. =end html
  4770. Source code for this example:
  4771. #!/usr/bin/perl -w
  4772. ###############################################################################
  4773. #
  4774. # Example of a sales worksheet to demonstrate several different features.
  4775. # Also uses functions from the L<Spreadsheet::WriteExcel::Utility> module.
  4776. #
  4777. # reverse('©'), October 2001, John McNamara, jmcnamara@cpan.org
  4778. #
  4779. use strict;
  4780. use Spreadsheet::WriteExcel;
  4781. use Spreadsheet::WriteExcel::Utility;
  4782. # Create a new workbook and add a worksheet
  4783. my $workbook = Spreadsheet::WriteExcel->new("sales.xls");
  4784. my $worksheet = $workbook->add_worksheet('May Sales');
  4785. # Set up some formats
  4786. my %heading = (
  4787. bold => 1,
  4788. pattern => 1,
  4789. fg_color => 19,
  4790. border => 1,
  4791. align => 'center',
  4792. );
  4793. my %total = (
  4794. bold => 1,
  4795. top => 1,
  4796. num_format => '$#,##0.00'
  4797. );
  4798. my $heading = $workbook->add_format(%heading);
  4799. my $total_format = $workbook->add_format(%total);
  4800. my $price_format = $workbook->add_format(num_format => '$#,##0.00');
  4801. my $date_format = $workbook->add_format(num_format => 'mmm d yyy');
  4802. # Write the main headings
  4803. $worksheet->freeze_panes(1); # Freeze the first row
  4804. $worksheet->write('A1', 'Item', $heading);
  4805. $worksheet->write('B1', 'Quantity', $heading);
  4806. $worksheet->write('C1', 'Price', $heading);
  4807. $worksheet->write('D1', 'Total', $heading);
  4808. $worksheet->write('E1', 'Date', $heading);
  4809. # Set the column widths
  4810. $worksheet->set_column('A:A', 25);
  4811. $worksheet->set_column('B:B', 10);
  4812. $worksheet->set_column('C:E', 16);
  4813. # Extract the sales data from the __DATA__ section at the end of the file.
  4814. # In reality this information would probably come from a database
  4815. my @sales;
  4816. foreach my $line (<DATA>) {
  4817. chomp $line;
  4818. next if $line eq '';
  4819. # Simple-minded processing of CSV data. Refer to the Text::CSV_XS
  4820. # and Text::xSV modules for a more complete CSV handling.
  4821. my @items = split /,/, $line;
  4822. push @sales, \@items;
  4823. }
  4824. # Write out the items from each row
  4825. my $row = 1;
  4826. foreach my $sale (@sales) {
  4827. $worksheet->write($row, 0, @$sale[0]);
  4828. $worksheet->write($row, 1, @$sale[1]);
  4829. $worksheet->write($row, 2, @$sale[2], $price_format);
  4830. # Create a formula like '=B2*C2'
  4831. my $formula = '='
  4832. . xl_rowcol_to_cell($row, 1)
  4833. . "*"
  4834. . xl_rowcol_to_cell($row, 2);
  4835. $worksheet->write($row, 3, $formula, $price_format);
  4836. # Parse the date
  4837. my $date = xl_decode_date_US(@$sale[3]);
  4838. $worksheet->write($row, 4, $date, $date_format);
  4839. $row++;
  4840. }
  4841. # Create a formula to sum the totals, like '=SUM(D2:D6)'
  4842. my $total = '=SUM(D2:'
  4843. . xl_rowcol_to_cell($row-1, 3)
  4844. . ")";
  4845. $worksheet->write($row, 3, $total, $total_format);
  4846. __DATA__
  4847. 586 card,20,125.50,5/12/01
  4848. Flat Screen Monitor,1,1300.00,5/12/01
  4849. 64 MB dimms,45,49.99,5/13/01
  4850. 15 GB HD,12,300.00,5/13/01
  4851. Speakers (pair),5,15.50,5/14/01
  4852. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/sales.pl>
  4853. =head2 Example: sendmail.pl
  4854. Example of how to use Mail::Sender to send a Spreadsheet::WriteExcel Excel
  4855. file as an attachment.
  4856. The main thing is to ensure that you close() the Worbook before you send it.
  4857. See the L<Mail::Sender> module for further details.
  4858. #!/usr/bin/perl -w
  4859. ###############################################################################
  4860. #
  4861. # Example of how to use Mail::Sender to send a Spreadsheet::WriteExcel Excel
  4862. # file as an attachment.
  4863. #
  4864. # The main thing is to ensure that you close() the Worbook before you send it.
  4865. #
  4866. # See the L<Mail::Sender> module for further details.
  4867. #
  4868. # reverse('©'), August 2002, John McNamara, jmcnamara@cpan.org
  4869. #
  4870. use strict;
  4871. use Spreadsheet::WriteExcel;
  4872. use Mail::Sender;
  4873. # Create an Excel file
  4874. my $workbook = Spreadsheet::WriteExcel->new("sendmail.xls");
  4875. my $worksheet = $workbook->add_worksheet;
  4876. $worksheet->write('A1', "Hello World!");
  4877. $workbook->close(); # Must close before sending
  4878. # Send the file. Change all variables to suit
  4879. my $sender = new Mail::Sender
  4880. {
  4881. smtp => '123.123.123.123',
  4882. from => 'Someone'
  4883. };
  4884. $sender->MailFile(
  4885. {
  4886. to => 'another@mail.com',
  4887. subject => 'Excel file',
  4888. msg => "Here is the data.\n",
  4889. file => 'mail.xls',
  4890. });
  4891. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/sendmail.pl>
  4892. =head2 Example: stats_ext.pl
  4893. Example of formatting using the Spreadsheet::WriteExcel module
  4894. This is a simple example of how to use functions that reference cells in
  4895. other worksheets within the same workbook.
  4896. =begin html
  4897. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/stats_ext.jpg" width="640" height="420" alt="Output from stats_ext.pl" /></center></p>
  4898. =end html
  4899. Source code for this example:
  4900. #!/usr/bin/perl -w
  4901. ###############################################################################
  4902. #
  4903. # Example of formatting using the Spreadsheet::WriteExcel module
  4904. #
  4905. # This is a simple example of how to use functions that reference cells in
  4906. # other worksheets within the same workbook.
  4907. #
  4908. # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org
  4909. #
  4910. use strict;
  4911. use Spreadsheet::WriteExcel;
  4912. # Create a new workbook and add a worksheet
  4913. my $workbook = Spreadsheet::WriteExcel->new("stats_ext.xls");
  4914. my $worksheet1 = $workbook->add_worksheet('Test results');
  4915. my $worksheet2 = $workbook->add_worksheet('Data');
  4916. # Set the column width for columns 1
  4917. $worksheet1->set_column('A:A', 20);
  4918. # Create a format for the headings
  4919. my $heading = $workbook->add_format();
  4920. $heading->set_bold();
  4921. # Create a numerical format
  4922. my $numformat = $workbook->add_format();
  4923. $numformat->set_num_format('0.00');
  4924. # Write some statistical functions
  4925. $worksheet1->write('A1', 'Count', $heading);
  4926. $worksheet1->write('B1', '=COUNT(Data!B2:B9)');
  4927. $worksheet1->write('A2', 'Sum', $heading);
  4928. $worksheet1->write('B2', '=SUM(Data!B2:B9)');
  4929. $worksheet1->write('A3', 'Average', $heading);
  4930. $worksheet1->write('B3', '=AVERAGE(Data!B2:B9)');
  4931. $worksheet1->write('A4', 'Min', $heading);
  4932. $worksheet1->write('B4', '=MIN(Data!B2:B9)');
  4933. $worksheet1->write('A5', 'Max', $heading);
  4934. $worksheet1->write('B5', '=MAX(Data!B2:B9)');
  4935. $worksheet1->write('A6', 'Standard Deviation', $heading);
  4936. $worksheet1->write('B6', '=STDEV(Data!B2:B9)');
  4937. $worksheet1->write('A7', 'Kurtosis', $heading);
  4938. $worksheet1->write('B7', '=KURT(Data!B2:B9)');
  4939. # Write the sample data
  4940. $worksheet2->write('A1', 'Sample', $heading);
  4941. $worksheet2->write('A2', 1);
  4942. $worksheet2->write('A3', 2);
  4943. $worksheet2->write('A4', 3);
  4944. $worksheet2->write('A5', 4);
  4945. $worksheet2->write('A6', 5);
  4946. $worksheet2->write('A7', 6);
  4947. $worksheet2->write('A8', 7);
  4948. $worksheet2->write('A9', 8);
  4949. $worksheet2->write('B1', 'Length', $heading);
  4950. $worksheet2->write('B2', 25.4, $numformat);
  4951. $worksheet2->write('B3', 25.4, $numformat);
  4952. $worksheet2->write('B4', 24.8, $numformat);
  4953. $worksheet2->write('B5', 25.0, $numformat);
  4954. $worksheet2->write('B6', 25.3, $numformat);
  4955. $worksheet2->write('B7', 24.9, $numformat);
  4956. $worksheet2->write('B8', 25.2, $numformat);
  4957. $worksheet2->write('B9', 24.8, $numformat);
  4958. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/stats_ext.pl>
  4959. =head2 Example: stocks.pl
  4960. Example of formatting using the Spreadsheet::WriteExcel module
  4961. This example shows how to use a conditional numerical format
  4962. with colours to indicate if a share price has gone up or down.
  4963. =begin html
  4964. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/stocks.jpg" width="640" height="420" alt="Output from stocks.pl" /></center></p>
  4965. =end html
  4966. Source code for this example:
  4967. #!/usr/bin/perl -w
  4968. ###############################################################################
  4969. #
  4970. # Example of formatting using the Spreadsheet::WriteExcel module
  4971. #
  4972. # This example shows how to use a conditional numerical format
  4973. # with colours to indicate if a share price has gone up or down.
  4974. #
  4975. # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org
  4976. #
  4977. use strict;
  4978. use Spreadsheet::WriteExcel;
  4979. # Create a new workbook and add a worksheet
  4980. my $workbook = Spreadsheet::WriteExcel->new("stocks.xls");
  4981. my $worksheet = $workbook->add_worksheet();
  4982. # Set the column width for columns 1, 2, 3 and 4
  4983. $worksheet->set_column(0, 3, 15);
  4984. # Create a format for the column headings
  4985. my $header = $workbook->add_format();
  4986. $header->set_bold();
  4987. $header->set_size(12);
  4988. $header->set_color('blue');
  4989. # Create a format for the stock price
  4990. my $f_price = $workbook->add_format();
  4991. $f_price->set_align('left');
  4992. $f_price->set_num_format('$0.00');
  4993. # Create a format for the stock volume
  4994. my $f_volume = $workbook->add_format();
  4995. $f_volume->set_align('left');
  4996. $f_volume->set_num_format('#,##0');
  4997. # Create a format for the price change. This is an example of a conditional
  4998. # format. The number is formatted as a percentage. If it is positive it is
  4999. # formatted in green, if it is negative it is formatted in red and if it is
  5000. # zero it is formatted as the default font colour (in this case black).
  5001. # Note: the [Green] format produces an unappealing lime green. Try
  5002. # [Color 10] instead for a dark green.
  5003. #
  5004. my $f_change = $workbook->add_format();
  5005. $f_change->set_align('left');
  5006. $f_change->set_num_format('[Green]0.0%;[Red]-0.0%;0.0%');
  5007. # Write out the data
  5008. $worksheet->write(0, 0, 'Company', $header);
  5009. $worksheet->write(0, 1, 'Price', $header);
  5010. $worksheet->write(0, 2, 'Volume', $header);
  5011. $worksheet->write(0, 3, 'Change', $header);
  5012. $worksheet->write(1, 0, 'Damage Inc.' );
  5013. $worksheet->write(1, 1, 30.25, $f_price); # $30.25
  5014. $worksheet->write(1, 2, 1234567, $f_volume); # 1,234,567
  5015. $worksheet->write(1, 3, 0.085, $f_change); # 8.5% in green
  5016. $worksheet->write(2, 0, 'Dump Corp.' );
  5017. $worksheet->write(2, 1, 1.56, $f_price); # $1.56
  5018. $worksheet->write(2, 2, 7564, $f_volume); # 7,564
  5019. $worksheet->write(2, 3, -0.015, $f_change); # -1.5% in red
  5020. $worksheet->write(3, 0, 'Rev Ltd.' );
  5021. $worksheet->write(3, 1, 0.13, $f_price); # $0.13
  5022. $worksheet->write(3, 2, 321, $f_volume); # 321
  5023. $worksheet->write(3, 3, 0, $f_change); # 0 in the font color (black)
  5024. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/stocks.pl>
  5025. =head2 Example: tab_colors.pl
  5026. Example of how to set Excel worksheet tab colours.
  5027. =begin html
  5028. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/tab_colors.jpg" width="640" height="420" alt="Output from tab_colors.pl" /></center></p>
  5029. =end html
  5030. Source code for this example:
  5031. #!/usr/bin/perl -w
  5032. #######################################################################
  5033. #
  5034. # Example of how to set Excel worksheet tab colours.
  5035. #
  5036. # reverse('©'), May 2006, John McNamara, jmcnamara@cpan.org
  5037. #
  5038. use strict;
  5039. use Spreadsheet::WriteExcel;
  5040. my $workbook = Spreadsheet::WriteExcel->new('tab_colors.xls');
  5041. my $worksheet1 = $workbook->add_worksheet();
  5042. my $worksheet2 = $workbook->add_worksheet();
  5043. my $worksheet3 = $workbook->add_worksheet();
  5044. my $worksheet4 = $workbook->add_worksheet();
  5045. # Worksheet1 will have the default tab colour.
  5046. $worksheet2->set_tab_color('red');
  5047. $worksheet3->set_tab_color('green');
  5048. $worksheet4->set_tab_color(0x35); # Orange
  5049. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/tab_colors.pl>
  5050. =head2 Example: textwrap.pl
  5051. Example of formatting using the Spreadsheet::WriteExcel module
  5052. This example shows how to wrap text in a cell. There are two alternatives,
  5053. vertical justification and text wrap.
  5054. With vertical justification the text is wrapped automatically to fit the
  5055. column width. With text wrap you must specify a newline with an embedded \n.
  5056. =begin html
  5057. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/textwrap.jpg" width="640" height="420" alt="Output from textwrap.pl" /></center></p>
  5058. =end html
  5059. Source code for this example:
  5060. #!/usr/bin/perl -w
  5061. ###############################################################################
  5062. #
  5063. # Example of formatting using the Spreadsheet::WriteExcel module
  5064. #
  5065. # This example shows how to wrap text in a cell. There are two alternatives,
  5066. # vertical justification and text wrap.
  5067. #
  5068. # With vertical justification the text is wrapped automatically to fit the
  5069. # column width. With text wrap you must specify a newline with an embedded \n.
  5070. #
  5071. # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org
  5072. #
  5073. use strict;
  5074. use Spreadsheet::WriteExcel;
  5075. # Create a new workbook and add a worksheet
  5076. my $workbook = Spreadsheet::WriteExcel->new("textwrap.xls");
  5077. my $worksheet = $workbook->add_worksheet();
  5078. # Set the column width for columns 1, 2 and 3
  5079. $worksheet->set_column(1, 1, 24);
  5080. $worksheet->set_column(2, 2, 34);
  5081. $worksheet->set_column(3, 3, 34);
  5082. # Set the row height for rows 1, 4, and 6. The height of row 2 will adjust
  5083. # automatically to fit the text.
  5084. #
  5085. $worksheet->set_row(0, 30);
  5086. $worksheet->set_row(3, 40);
  5087. $worksheet->set_row(5, 80);
  5088. # No newlines
  5089. my $str1 = "For whatever we lose (like a you or a me) ";
  5090. $str1 .= "it's always ourselves we find in the sea";
  5091. # Embedded newlines
  5092. my $str2 = "For whatever we lose\n(like a you or a me)\n";
  5093. $str2 .= "it's always ourselves\nwe find in the sea";
  5094. # Create a format for the column headings
  5095. my $header = $workbook->add_format();
  5096. $header->set_bold();
  5097. $header->set_font("Courier New");
  5098. $header->set_align('center');
  5099. $header->set_align('vcenter');
  5100. # Create a "vertical justification" format
  5101. my $format1 = $workbook->add_format();
  5102. $format1->set_align('vjustify');
  5103. # Create a "text wrap" format
  5104. my $format2 = $workbook->add_format();
  5105. $format2->set_text_wrap();
  5106. # Write the headers
  5107. $worksheet->write(0, 1, "set_align('vjustify')", $header);
  5108. $worksheet->write(0, 2, "set_align('vjustify')", $header);
  5109. $worksheet->write(0, 3, "set_text_wrap()", $header);
  5110. # Write some examples
  5111. $worksheet->write(1, 1, $str1, $format1);
  5112. $worksheet->write(1, 2, $str1, $format1);
  5113. $worksheet->write(1, 3, $str2, $format2);
  5114. $worksheet->write(3, 1, $str1, $format1);
  5115. $worksheet->write(3, 2, $str1, $format1);
  5116. $worksheet->write(3, 3, $str2, $format2);
  5117. $worksheet->write(5, 1, $str1, $format1);
  5118. $worksheet->write(5, 2, $str1, $format1);
  5119. $worksheet->write(5, 3, $str2, $format2);
  5120. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/textwrap.pl>
  5121. =head2 Example: win32ole.pl
  5122. This is a simple example of how to create an Excel file using the
  5123. Win32::OLE module for the sake of comparison.
  5124. #!/usr/bin/perl -w
  5125. ###############################################################################
  5126. #
  5127. # This is a simple example of how to create an Excel file using the
  5128. # Win32::OLE module for the sake of comparison.
  5129. #
  5130. # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org
  5131. #
  5132. use strict;
  5133. use Cwd;
  5134. use Win32::OLE;
  5135. use Win32::OLE::Const 'Microsoft Excel';
  5136. my $application = Win32::OLE->new("Excel.Application");
  5137. my $workbook = $application->Workbooks->Add;
  5138. my $worksheet = $workbook->Worksheets(1);
  5139. $worksheet->Cells(1,1)->{Value} = "Hello World";
  5140. $worksheet->Cells(2,1)->{Value} = "One";
  5141. $worksheet->Cells(3,1)->{Value} = "Two";
  5142. $worksheet->Cells(4,1)->{Value} = 3;
  5143. $worksheet->Cells(5,1)->{Value} = 4.0000001;
  5144. # Add some formatting
  5145. $worksheet->Cells(1,1)->Font->{Bold} = "True";
  5146. $worksheet->Cells(1,1)->Font->{Size} = 16;
  5147. $worksheet->Cells(1,1)->Font->{ColorIndex} = 3;
  5148. $worksheet->Columns("A:A")->{ColumnWidth} = 25;
  5149. # Write a hyperlink
  5150. my $range = $worksheet->Range("A7:A7");
  5151. $worksheet->Hyperlinks->Add({ Anchor => $range, Address => "http://www.perl.com/"});
  5152. # Get current directory using Cwd.pm
  5153. my $dir = cwd();
  5154. $workbook->SaveAs({
  5155. FileName => $dir . '/win32ole.xls',
  5156. FileFormat => xlNormal,
  5157. });
  5158. $workbook->Close;
  5159. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/win32ole.pl>
  5160. =head2 Example: write_arrays.pl
  5161. Example of how to use the Spreadsheet::WriteExcel module to
  5162. write 1D and 2D arrays of data.
  5163. To find out more about array references refer(!!) to the perlref and
  5164. perlreftut manpages. To find out more about 2D arrays or "list of
  5165. lists" refer to the perllol manpage.
  5166. =begin html
  5167. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/write_arrays.jpg" width="640" height="420" alt="Output from write_arrays.pl" /></center></p>
  5168. =end html
  5169. Source code for this example:
  5170. #!/usr/bin/perl -w
  5171. #######################################################################
  5172. #
  5173. # Example of how to use the Spreadsheet::WriteExcel module to
  5174. # write 1D and 2D arrays of data.
  5175. #
  5176. # To find out more about array references refer(!!) to the perlref and
  5177. # perlreftut manpages. To find out more about 2D arrays or "list of
  5178. # lists" refer to the perllol manpage.
  5179. #
  5180. # reverse('©'), March 2002, John McNamara, jmcnamara@cpan.org
  5181. #
  5182. use strict;
  5183. use Spreadsheet::WriteExcel;
  5184. my $workbook = Spreadsheet::WriteExcel->new("write_arrays.xls");
  5185. my $worksheet1 = $workbook->add_worksheet('Example 1');
  5186. my $worksheet2 = $workbook->add_worksheet('Example 2');
  5187. my $worksheet3 = $workbook->add_worksheet('Example 3');
  5188. my $worksheet4 = $workbook->add_worksheet('Example 4');
  5189. my $worksheet5 = $workbook->add_worksheet('Example 5');
  5190. my $worksheet6 = $workbook->add_worksheet('Example 6');
  5191. my $worksheet7 = $workbook->add_worksheet('Example 7');
  5192. my $worksheet8 = $workbook->add_worksheet('Example 8');
  5193. my $format = $workbook->add_format(color => 'red', bold => 1);
  5194. # Data arrays used in the following examples.
  5195. # undef values are written as blank cells (with format if specified).
  5196. #
  5197. my @array = ( 'one', 'two', undef, 'four' );
  5198. my @array2d = (
  5199. ['maggie', 'milly', 'molly', 'may' ],
  5200. [13, 14, 15, 16 ],
  5201. ['shell', 'star', 'crab', 'stone'],
  5202. );
  5203. # 1. Write a row of data using an array reference.
  5204. $worksheet1->write('A1', \@array);
  5205. # 2. Same as 1. above using an anonymous array ref.
  5206. $worksheet2->write('A1', [ @array ]);
  5207. # 3. Write a row of data using an explicit write_row() method call.
  5208. # This is the same as calling write() in Ex. 1 above.
  5209. #
  5210. $worksheet3->write_row('A1', \@array);
  5211. # 4. Write a column of data using the write_col() method call.
  5212. $worksheet4->write_col('A1', \@array);
  5213. # 5. Write a column of data using a ref to an array ref, i.e. a 2D array.
  5214. $worksheet5->write('A1', [ \@array ]);
  5215. # 6. Write a 2D array in col-row order.
  5216. $worksheet6->write('A1', \@array2d);
  5217. # 7. Write a 2D array in row-col order.
  5218. $worksheet7->write_col('A1', \@array2d);
  5219. # 8. Write a row of data with formatting. The blank cell is also formatted.
  5220. $worksheet8->write('A1', \@array, $format);
  5221. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/write_arrays.pl>
  5222. =head2 Example: write_handler1.pl
  5223. Example of how to add a user defined data handler to the Spreadsheet::
  5224. WriteExcel write() method.
  5225. The following example shows how to add a handler for a 7 digit ID number.
  5226. =begin html
  5227. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/write_handler1.jpg" width="640" height="420" alt="Output from write_handler1.pl" /></center></p>
  5228. =end html
  5229. Source code for this example:
  5230. #!/usr/bin/perl -w
  5231. ###############################################################################
  5232. #
  5233. # Example of how to add a user defined data handler to the Spreadsheet::
  5234. # WriteExcel write() method.
  5235. #
  5236. # The following example shows how to add a handler for a 7 digit ID number.
  5237. #
  5238. #
  5239. # reverse('©'), September 2004, John McNamara, jmcnamara@cpan.org
  5240. #
  5241. use strict;
  5242. use Spreadsheet::WriteExcel;
  5243. my $workbook = Spreadsheet::WriteExcel->new("write_handler1.xls");
  5244. my $worksheet = $workbook->add_worksheet();
  5245. ###############################################################################
  5246. #
  5247. # Add a handler for 7 digit id numbers. This is useful when you want a string
  5248. # such as 0000001 written as a string instead of a number and thus preserve
  5249. # the leading zeroes.
  5250. #
  5251. # Note: you can get the same effect using the keep_leading_zeros() method but
  5252. # this serves as a simple example.
  5253. #
  5254. $worksheet->add_write_handler(qr[^\d{7}$], \&write_my_id);
  5255. ###############################################################################
  5256. #
  5257. # The following function processes the data when a match is found.
  5258. #
  5259. sub write_my_id {
  5260. my $worksheet = shift;
  5261. return $worksheet->write_string(@_);
  5262. }
  5263. # This format maintains the cell as text even if it is edited.
  5264. my $id_format = $workbook->add_format(num_format => '@');
  5265. # Write some numbers in the user defined format
  5266. $worksheet->write('A1', '0000000', $id_format);
  5267. $worksheet->write('A2', '0000001', $id_format);
  5268. $worksheet->write('A3', '0004000', $id_format);
  5269. $worksheet->write('A4', '1234567', $id_format);
  5270. # Write some numbers that don't match the defined format
  5271. $worksheet->write('A6', '000000', $id_format);
  5272. $worksheet->write('A7', '000001', $id_format);
  5273. $worksheet->write('A8', '004000', $id_format);
  5274. $worksheet->write('A9', '123456', $id_format);
  5275. __END__
  5276. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/write_handler1.pl>
  5277. =head2 Example: write_handler2.pl
  5278. Example of how to add a user defined data handler to the Spreadsheet::
  5279. WriteExcel write() method.
  5280. The following example shows how to add a handler for a 7 digit ID number.
  5281. It adds an additional constraint to the write_handler1.pl in that it only
  5282. filters data that isn't in the third column.
  5283. =begin html
  5284. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/write_handler2.jpg" width="640" height="420" alt="Output from write_handler2.pl" /></center></p>
  5285. =end html
  5286. Source code for this example:
  5287. #!/usr/bin/perl -w
  5288. ###############################################################################
  5289. #
  5290. # Example of how to add a user defined data handler to the Spreadsheet::
  5291. # WriteExcel write() method.
  5292. #
  5293. # The following example shows how to add a handler for a 7 digit ID number.
  5294. # It adds an additional constraint to the write_handler1.pl in that it only
  5295. # filters data that isn't in the third column.
  5296. #
  5297. #
  5298. # reverse('©'), September 2004, John McNamara, jmcnamara@cpan.org
  5299. #
  5300. use strict;
  5301. use Spreadsheet::WriteExcel;
  5302. my $workbook = Spreadsheet::WriteExcel->new("write_handler2.xls");
  5303. my $worksheet = $workbook->add_worksheet();
  5304. ###############################################################################
  5305. #
  5306. # Add a handler for 7 digit id numbers. This is useful when you want a string
  5307. # such as 0000001 written as a string instead of a number and thus preserve
  5308. # the leading zeroes.
  5309. #
  5310. # Note: you can get the same effect using the keep_leading_zeros() method but
  5311. # this serves as a simple example.
  5312. #
  5313. $worksheet->add_write_handler(qr[^\d{7}$], \&write_my_id);
  5314. ###############################################################################
  5315. #
  5316. # The following function processes the data when a match is found. The handler
  5317. # is set up so that it only filters data if it is in the third column.
  5318. #
  5319. sub write_my_id {
  5320. my $worksheet = shift;
  5321. my $col = $_[1];
  5322. # col is zero based
  5323. if ($col != 2) {
  5324. return $worksheet->write_string(@_);
  5325. }
  5326. else {
  5327. # Reject the match and return control to write()
  5328. return undef;
  5329. }
  5330. }
  5331. # This format maintains the cell as text even if it is edited.
  5332. my $id_format = $workbook->add_format(num_format => '@');
  5333. # Write some numbers in the user defined format
  5334. $worksheet->write('A1', '0000000', $id_format);
  5335. $worksheet->write('B1', '0000001', $id_format);
  5336. $worksheet->write('C1', '0000002', $id_format);
  5337. $worksheet->write('D1', '0000003', $id_format);
  5338. __END__
  5339. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/write_handler2.pl>
  5340. =head2 Example: write_handler3.pl
  5341. Example of how to add a user defined data handler to the Spreadsheet::
  5342. WriteExcel write() method.
  5343. The following example shows how to add a handler for dates in a specific
  5344. format.
  5345. See write_handler4.pl for a more rigorous example with error handling.
  5346. =begin html
  5347. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/write_handler3.jpg" width="640" height="420" alt="Output from write_handler3.pl" /></center></p>
  5348. =end html
  5349. Source code for this example:
  5350. #!/usr/bin/perl -w
  5351. ###############################################################################
  5352. #
  5353. # Example of how to add a user defined data handler to the Spreadsheet::
  5354. # WriteExcel write() method.
  5355. #
  5356. # The following example shows how to add a handler for dates in a specific
  5357. # format.
  5358. #
  5359. # See write_handler4.pl for a more rigorous example with error handling.
  5360. #
  5361. # reverse('©'), September 2004, John McNamara, jmcnamara@cpan.org
  5362. #
  5363. use strict;
  5364. use Spreadsheet::WriteExcel;
  5365. my $workbook = Spreadsheet::WriteExcel->new("write_handler3.xls");
  5366. my $worksheet = $workbook->add_worksheet();
  5367. my $date_format = $workbook->add_format(num_format => 'dd/mm/yy');
  5368. ###############################################################################
  5369. #
  5370. # Add a handler to match dates in the following format: d/m/yyyy
  5371. #
  5372. # The day and month can be single or double digits.
  5373. #
  5374. $worksheet->add_write_handler(qr[^\d{1,2}/\d{1,2}/\d{4}$], \&write_my_date);
  5375. ###############################################################################
  5376. #
  5377. # The following function processes the data when a match is found.
  5378. # See write_handler4.pl for a more rigorous example with error handling.
  5379. #
  5380. sub write_my_date {
  5381. my $worksheet = shift;
  5382. my @args = @_;
  5383. my $token = $args[2];
  5384. $token =~ qr[^(\d{1,2})/(\d{1,2})/(\d{4})$];
  5385. # Change to the date format required by write_date_time().
  5386. my $date = sprintf "%4d-%02d-%02dT", $3, $2, $1;
  5387. $args[2] = $date;
  5388. return $worksheet->write_date_time(@args);
  5389. }
  5390. # Write some dates in the user defined format
  5391. $worksheet->write('A1', '22/12/2004', $date_format);
  5392. $worksheet->write('A2', '1/1/1995', $date_format);
  5393. $worksheet->write('A3', '01/01/1995', $date_format);
  5394. __END__
  5395. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/write_handler3.pl>
  5396. =head2 Example: write_handler4.pl
  5397. Example of how to add a user defined data handler to the Spreadsheet::
  5398. WriteExcel write() method.
  5399. The following example shows how to add a handler for dates in a specific
  5400. format.
  5401. This is a more rigorous version of write_handler3.pl.
  5402. =begin html
  5403. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/write_handler4.jpg" width="640" height="420" alt="Output from write_handler4.pl" /></center></p>
  5404. =end html
  5405. Source code for this example:
  5406. #!/usr/bin/perl -w
  5407. ###############################################################################
  5408. #
  5409. # Example of how to add a user defined data handler to the Spreadsheet::
  5410. # WriteExcel write() method.
  5411. #
  5412. # The following example shows how to add a handler for dates in a specific
  5413. # format.
  5414. #
  5415. # This is a more rigorous version of write_handler3.pl.
  5416. #
  5417. # reverse('©'), September 2004, John McNamara, jmcnamara@cpan.org
  5418. #
  5419. use strict;
  5420. use Spreadsheet::WriteExcel;
  5421. my $workbook = Spreadsheet::WriteExcel->new("write_handler4.xls");
  5422. my $worksheet = $workbook->add_worksheet();
  5423. my $date_format = $workbook->add_format(num_format => 'dd/mm/yy');
  5424. ###############################################################################
  5425. #
  5426. # Add a handler to match dates in the following formats: d/m/yy, d/m/yyyy
  5427. #
  5428. # The day and month can be single or double digits and the year can be 2 or 4
  5429. # digits.
  5430. #
  5431. $worksheet->add_write_handler(qr[^\d{1,2}/\d{1,2}/\d{2,4}$], \&write_my_date);
  5432. ###############################################################################
  5433. #
  5434. # The following function processes the data when a match is found.
  5435. #
  5436. sub write_my_date {
  5437. my $worksheet = shift;
  5438. my @args = @_;
  5439. my $token = $args[2];
  5440. if ($token =~ qr[^(\d{1,2})/(\d{1,2})/(\d{2,4})$]) {
  5441. my $day = $1;
  5442. my $mon = $2;
  5443. my $year = $3;
  5444. # Use a window for 2 digit dates. This will keep some ragged Perl
  5445. # programmer employed in thirty years time. :-)
  5446. if (length $year == 2) {
  5447. if ($year < 50) {
  5448. $year += 2000;
  5449. }
  5450. else {
  5451. $year += 1900;
  5452. }
  5453. }
  5454. my $date = sprintf "%4d-%02d-%02dT", $year, $mon, $day;
  5455. # Convert the ISO ISO8601 style string to an Excel date
  5456. $date = $worksheet->convert_date_time($date);
  5457. if (defined $date) {
  5458. # Date was valid
  5459. $args[2] = $date;
  5460. return $worksheet->write_number(@args);
  5461. }
  5462. else {
  5463. # Not a valid date therefore write as a string
  5464. return $worksheet->write_string(@args);
  5465. }
  5466. }
  5467. else {
  5468. # Shouldn't happen if the same match is used in the re and sub.
  5469. return undef;
  5470. }
  5471. }
  5472. # Write some dates in the user defined format
  5473. $worksheet->write('A1', '22/12/2004', $date_format);
  5474. $worksheet->write('A2', '22/12/04', $date_format);
  5475. $worksheet->write('A3', '2/12/04', $date_format);
  5476. $worksheet->write('A4', '2/5/04', $date_format);
  5477. $worksheet->write('A5', '2/5/95', $date_format);
  5478. $worksheet->write('A6', '2/5/1995', $date_format);
  5479. # Some erroneous dates
  5480. $worksheet->write('A8', '2/5/1895', $date_format); # Date out of Excel range
  5481. $worksheet->write('A9', '29/2/2003', $date_format); # Invalid leap day
  5482. $worksheet->write('A10','50/50/50', $date_format); # Matches but isn't a date
  5483. __END__
  5484. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/write_handler4.pl>
  5485. =head2 Example: write_to_scalar.pl
  5486. An example of writing an Excel file to a Perl scalar using Spreadsheet::
  5487. WriteExcel and the new features of perl 5.8.
  5488. For an examples of how to write to a scalar in versions prior to perl 5.8
  5489. see the filehandle.pl program and IO:Scalar.
  5490. #!/usr/bin/perl -w
  5491. ##############################################################################
  5492. #
  5493. # An example of writing an Excel file to a Perl scalar using Spreadsheet::
  5494. # WriteExcel and the new features of perl 5.8.
  5495. #
  5496. # For an examples of how to write to a scalar in versions prior to perl 5.8
  5497. # see the filehandle.pl program and IO:Scalar.
  5498. #
  5499. # reverse('©'), September 2004, John McNamara, jmcnamara@cpan.org
  5500. #
  5501. use strict;
  5502. use Spreadsheet::WriteExcel;
  5503. require 5.008;
  5504. # Use perl 5.8's feature of using a scalar as a filehandle.
  5505. my $fh;
  5506. my $str = '';
  5507. open $fh, '>', \$str or die "Failed to open filehandle: $!";;
  5508. # Or replace the previous three lines with this:
  5509. # open my $fh, '>', \my $str or die "Failed to open filehandle: $!";
  5510. # Spreadsheet::WriteExce accepts filehandle as well as file names.
  5511. my $workbook = Spreadsheet::WriteExcel->new($fh);
  5512. my $worksheet = $workbook->add_worksheet();
  5513. $worksheet->write(0, 0, "Hi Excel!");
  5514. $workbook->close();
  5515. # The Excel file in now in $str. Remember to binmode() the output
  5516. # filehandle before printing it.
  5517. binmode STDOUT;
  5518. print $str;
  5519. __END__
  5520. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/write_to_scalar.pl>
  5521. =head2 Example: unicode_utf16.pl
  5522. A simple example of writing some Unicode text with Spreadsheet::WriteExcel.
  5523. This example shows UTF16 encoding. With perl 5.8 it is also possible to use
  5524. utf8 without modification.
  5525. =begin html
  5526. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/unicode_utf16.jpg" width="640" height="420" alt="Output from unicode_utf16.pl" /></center></p>
  5527. =end html
  5528. Source code for this example:
  5529. #!/usr/bin/perl -w
  5530. ##############################################################################
  5531. #
  5532. # A simple example of writing some Unicode text with Spreadsheet::WriteExcel.
  5533. #
  5534. # This example shows UTF16 encoding. With perl 5.8 it is also possible to use
  5535. # utf8 without modification.
  5536. #
  5537. # reverse('©'), May 2004, John McNamara, jmcnamara@cpan.org
  5538. #
  5539. use strict;
  5540. use Spreadsheet::WriteExcel;
  5541. my $workbook = Spreadsheet::WriteExcel->new('unicode_utf16.xls');
  5542. my $worksheet = $workbook->add_worksheet();
  5543. # Write the Unicode smiley face (with increased font for legibility)
  5544. my $smiley = pack "n", 0x263a;
  5545. my $big_font = $workbook->add_format(size => 40);
  5546. $worksheet->write_utf16be_string('A3', $smiley, $big_font);
  5547. # Write a phrase in Cyrillic
  5548. my $uni_str = pack "H*", "042d0442043e002004440440043004370430002004".
  5549. "3d043000200440044304410441043a043e043c0021";
  5550. $worksheet->write_utf16be_string('A5', $uni_str);
  5551. $worksheet->write_utf16be_string('A7', pack "H*", "0074006500730074");
  5552. __END__
  5553. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/unicode_utf16.pl>
  5554. =head2 Example: unicode_utf16_japan.pl
  5555. A simple example of writing some Unicode text with Spreadsheet::WriteExcel.
  5556. This creates an Excel file with the word Nippon in 3 character sets.
  5557. This example shows UTF16 encoding. With perl 5.8 it is also possible to use
  5558. utf8 without modification.
  5559. See also the unicode_2022_jp.pl and unicode_shift_jis.pl examples.
  5560. =begin html
  5561. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/unicode_utf16_japan.jpg" width="640" height="420" alt="Output from unicode_utf16_japan.pl" /></center></p>
  5562. =end html
  5563. Source code for this example:
  5564. #!/usr/bin/perl -w
  5565. ##############################################################################
  5566. #
  5567. # A simple example of writing some Unicode text with Spreadsheet::WriteExcel.
  5568. #
  5569. # This creates an Excel file with the word Nippon in 3 character sets.
  5570. #
  5571. # This example shows UTF16 encoding. With perl 5.8 it is also possible to use
  5572. # utf8 without modification.
  5573. #
  5574. # See also the unicode_2022_jp.pl and unicode_shift_jis.pl examples.
  5575. #
  5576. # reverse('©'), May 2004, John McNamara, jmcnamara@cpan.org
  5577. #
  5578. use strict;
  5579. use Spreadsheet::WriteExcel;
  5580. my $workbook = Spreadsheet::WriteExcel->new('unicode_utf16_japan.xls');
  5581. my $worksheet = $workbook->add_worksheet();
  5582. # Set a Unicode font.
  5583. my $uni_font = $workbook->add_format(font => 'Arial Unicode MS');
  5584. # Create some UTF-16BE Unicode text.
  5585. my $kanji = pack 'n*', 0x65e5, 0x672c;
  5586. my $katakana = pack 'n*', 0xff86, 0xff8e, 0xff9d;
  5587. my $hiragana = pack 'n*', 0x306b, 0x307b, 0x3093;
  5588. $worksheet->write_utf16be_string('A1', $kanji, $uni_font);
  5589. $worksheet->write_utf16be_string('A2', $katakana, $uni_font);
  5590. $worksheet->write_utf16be_string('A3', $hiragana, $uni_font);
  5591. $worksheet->write('B1', 'Kanji');
  5592. $worksheet->write('B2', 'Katakana');
  5593. $worksheet->write('B3', 'Hiragana');
  5594. __END__
  5595. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/unicode_utf16_japan.pl>
  5596. =head2 Example: unicode_cyrillic.pl
  5597. A simple example of writing some Russian cyrillic text using
  5598. Spreadsheet::WriteExcel and perl 5.8.
  5599. =begin html
  5600. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/unicode_cyrillic.jpg" width="640" height="420" alt="Output from unicode_cyrillic.pl" /></center></p>
  5601. =end html
  5602. Source code for this example:
  5603. #!/usr/bin/perl -w
  5604. ##############################################################################
  5605. #
  5606. # A simple example of writing some Russian cyrillic text using
  5607. # Spreadsheet::WriteExcel and perl 5.8.
  5608. #
  5609. # reverse('©'), March 2005, John McNamara, jmcnamara@cpan.org
  5610. #
  5611. # Perl 5.8 or later is required for proper utf8 handling. For older perl
  5612. # versions you should use UTF16 and the write_utf16be_string() method.
  5613. # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs.
  5614. #
  5615. require 5.008;
  5616. use strict;
  5617. use Spreadsheet::WriteExcel;
  5618. # In this example we generate utf8 strings from character data but in a
  5619. # real application we would expect them to come from an external source.
  5620. #
  5621. # Create a Russian worksheet name in utf8.
  5622. my $sheet = pack "U*", 0x0421, 0x0442, 0x0440, 0x0430, 0x043D, 0x0438,
  5623. 0x0446, 0x0430;
  5624. # Create a Russian string.
  5625. my $str = pack "U*", 0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441,
  5626. 0x0442, 0x0432, 0x0443, 0x0439, 0x0020, 0x041C,
  5627. 0x0438, 0x0440, 0x0021;
  5628. my $workbook = Spreadsheet::WriteExcel->new("unicode_cyrillic.xls");
  5629. my $worksheet = $workbook->add_worksheet($sheet . '1');
  5630. $worksheet->set_column('A:A', 18);
  5631. $worksheet->write('A1', $str);
  5632. __END__
  5633. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/unicode_cyrillic.pl>
  5634. =head2 Example: unicode_list.pl
  5635. A simple example using Spreadsheet::WriteExcel to display all available
  5636. Unicode characters in a font.
  5637. =begin html
  5638. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/unicode_list.jpg" width="640" height="420" alt="Output from unicode_list.pl" /></center></p>
  5639. =end html
  5640. Source code for this example:
  5641. #!/usr/bin/perl -w
  5642. ##############################################################################
  5643. #
  5644. # A simple example using Spreadsheet::WriteExcel to display all available
  5645. # Unicode characters in a font.
  5646. #
  5647. # reverse('©'), May 2004, John McNamara, jmcnamara@cpan.org
  5648. #
  5649. use strict;
  5650. use Spreadsheet::WriteExcel;
  5651. my $workbook = Spreadsheet::WriteExcel->new('unicode_list.xls');
  5652. my $worksheet = $workbook->add_worksheet();
  5653. # Set a Unicode font.
  5654. my $uni_font = $workbook->add_format(font => 'Arial Unicode MS');
  5655. # Ascii font for labels.
  5656. my $courier = $workbook->add_format(font => 'Courier New');
  5657. my $char = 0;
  5658. # Loop through all 32768 UTF-16BE characters.
  5659. #
  5660. for my $row (0 .. 2 ** 12 -1) {
  5661. for my $col (0 .. 31) {
  5662. last if $char == 0xffff;
  5663. if ($col % 2 == 0){
  5664. $worksheet->write_string($row, $col,
  5665. sprintf('0x%04X', $char), $courier);
  5666. }
  5667. else {
  5668. $worksheet->write_utf16be_string($row, $col,
  5669. pack('n', $char++), $uni_font);
  5670. }
  5671. }
  5672. }
  5673. __END__
  5674. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/unicode_list.pl>
  5675. =head2 Example: unicode_2022_jp.pl
  5676. A simple example of converting some Unicode text to an Excel file using
  5677. Spreadsheet::WriteExcel and perl 5.8.
  5678. This example generates some Japanese from a file with ISO-2022-JP
  5679. encoded text.
  5680. =begin html
  5681. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/unicode_2022_jp.jpg" width="640" height="420" alt="Output from unicode_2022_jp.pl" /></center></p>
  5682. =end html
  5683. Source code for this example:
  5684. #!/usr/bin/perl -w
  5685. ##############################################################################
  5686. #
  5687. # A simple example of converting some Unicode text to an Excel file using
  5688. # Spreadsheet::WriteExcel and perl 5.8.
  5689. #
  5690. # This example generates some Japanese from a file with ISO-2022-JP
  5691. # encoded text.
  5692. #
  5693. # reverse('©'), September 2004, John McNamara, jmcnamara@cpan.org
  5694. #
  5695. # Perl 5.8 or later is required for proper utf8 handling. For older perl
  5696. # versions you should use UTF16 and the write_utf16be_string() method.
  5697. # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs.
  5698. #
  5699. require 5.008;
  5700. use strict;
  5701. use Spreadsheet::WriteExcel;
  5702. my $workbook = Spreadsheet::WriteExcel->new("unicode_2022_jp.xls");
  5703. my $worksheet = $workbook->add_worksheet();
  5704. $worksheet->set_column('A:A', 50);
  5705. my $file = 'unicode_2022_jp.txt';
  5706. open FH, '<:encoding(iso-2022-jp)', $file or die "Couldn't open $file: $!\n";
  5707. my $row = 0;
  5708. while (<FH>) {
  5709. next if /^#/; # Ignore the comments in the sample file.
  5710. chomp;
  5711. $worksheet->write($row++, 0, $_);
  5712. }
  5713. __END__
  5714. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/unicode_2022_jp.pl>
  5715. =head2 Example: unicode_8859_11.pl
  5716. A simple example of converting some Unicode text to an Excel file using
  5717. Spreadsheet::WriteExcel and perl 5.8.
  5718. This example generates some Thai from a file with ISO-8859-11 encoded text.
  5719. =begin html
  5720. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/unicode_8859_11.jpg" width="640" height="420" alt="Output from unicode_8859_11.pl" /></center></p>
  5721. =end html
  5722. Source code for this example:
  5723. #!/usr/bin/perl -w
  5724. ##############################################################################
  5725. #
  5726. # A simple example of converting some Unicode text to an Excel file using
  5727. # Spreadsheet::WriteExcel and perl 5.8.
  5728. #
  5729. # This example generates some Thai from a file with ISO-8859-11 encoded text.
  5730. #
  5731. #
  5732. # reverse('©'), September 2004, John McNamara, jmcnamara@cpan.org
  5733. #
  5734. # Perl 5.8 or later is required for proper utf8 handling. For older perl
  5735. # versions you should use UTF16 and the write_utf16be_string() method.
  5736. # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs.
  5737. #
  5738. require 5.008;
  5739. use strict;
  5740. use Spreadsheet::WriteExcel;
  5741. my $workbook = Spreadsheet::WriteExcel->new("unicode_8859_11.xls");
  5742. my $worksheet = $workbook->add_worksheet();
  5743. $worksheet->set_column('A:A', 50);
  5744. my $file = 'unicode_8859_11.txt';
  5745. open FH, '<:encoding(iso-8859-11)', $file or die "Couldn't open $file: $!\n";
  5746. my $row = 0;
  5747. while (<FH>) {
  5748. next if /^#/; # Ignore the comments in the sample file.
  5749. chomp;
  5750. $worksheet->write($row++, 0, $_);
  5751. }
  5752. __END__
  5753. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/unicode_8859_11.pl>
  5754. =head2 Example: unicode_8859_7.pl
  5755. A simple example of converting some Unicode text to an Excel file using
  5756. Spreadsheet::WriteExcel and perl 5.8.
  5757. This example generates some Greek from a file with ISO-8859-7 encoded text.
  5758. =begin html
  5759. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/unicode_8859_7.jpg" width="640" height="420" alt="Output from unicode_8859_7.pl" /></center></p>
  5760. =end html
  5761. Source code for this example:
  5762. #!/usr/bin/perl -w
  5763. ##############################################################################
  5764. #
  5765. # A simple example of converting some Unicode text to an Excel file using
  5766. # Spreadsheet::WriteExcel and perl 5.8.
  5767. #
  5768. # This example generates some Greek from a file with ISO-8859-7 encoded text.
  5769. #
  5770. #
  5771. # reverse('©'), September 2004, John McNamara, jmcnamara@cpan.org
  5772. #
  5773. # Perl 5.8 or later is required for proper utf8 handling. For older perl
  5774. # versions you should use UTF16 and the write_utf16be_string() method.
  5775. # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs.
  5776. #
  5777. require 5.008;
  5778. use strict;
  5779. use Spreadsheet::WriteExcel;
  5780. my $workbook = Spreadsheet::WriteExcel->new("unicode_8859_7.xls");
  5781. my $worksheet = $workbook->add_worksheet();
  5782. $worksheet->set_column('A:A', 50);
  5783. my $file = 'unicode_8859_7.txt';
  5784. open FH, '<:encoding(iso-8859-7)', $file or die "Couldn't open $file: $!\n";
  5785. my $row = 0;
  5786. while (<FH>) {
  5787. next if /^#/; # Ignore the comments in the sample file.
  5788. chomp;
  5789. $worksheet->write($row++, 0, $_);
  5790. }
  5791. __END__
  5792. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/unicode_8859_7.pl>
  5793. =head2 Example: unicode_big5.pl
  5794. A simple example of converting some Unicode text to an Excel file using
  5795. Spreadsheet::WriteExcel and perl 5.8.
  5796. This example generates some Chinese from a file with BIG5 encoded text.
  5797. =begin html
  5798. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/unicode_big5.jpg" width="640" height="420" alt="Output from unicode_big5.pl" /></center></p>
  5799. =end html
  5800. Source code for this example:
  5801. #!/usr/bin/perl -w
  5802. ##############################################################################
  5803. #
  5804. # A simple example of converting some Unicode text to an Excel file using
  5805. # Spreadsheet::WriteExcel and perl 5.8.
  5806. #
  5807. # This example generates some Chinese from a file with BIG5 encoded text.
  5808. #
  5809. #
  5810. # reverse('©'), September 2004, John McNamara, jmcnamara@cpan.org
  5811. #
  5812. # Perl 5.8 or later is required for proper utf8 handling. For older perl
  5813. # versions you should use UTF16 and the write_utf16be_string() method.
  5814. # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs.
  5815. #
  5816. require 5.008;
  5817. use strict;
  5818. use Spreadsheet::WriteExcel;
  5819. my $workbook = Spreadsheet::WriteExcel->new("unicode_big5.xls");
  5820. my $worksheet = $workbook->add_worksheet();
  5821. $worksheet->set_column('A:A', 80);
  5822. my $file = 'unicode_big5.txt';
  5823. open FH, '<:encoding(big5)', $file or die "Couldn't open $file: $!\n";
  5824. my $row = 0;
  5825. while (<FH>) {
  5826. next if /^#/; # Ignore the comments in the sample file.
  5827. chomp;
  5828. $worksheet->write($row++, 0, $_);
  5829. }
  5830. __END__
  5831. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/unicode_big5.pl>
  5832. =head2 Example: unicode_cp1251.pl
  5833. A simple example of converting some Unicode text to an Excel file using
  5834. Spreadsheet::WriteExcel and perl 5.8.
  5835. This example generates some Russian from a file with CP1251 encoded text.
  5836. =begin html
  5837. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/unicode_cp1251.jpg" width="640" height="420" alt="Output from unicode_cp1251.pl" /></center></p>
  5838. =end html
  5839. Source code for this example:
  5840. #!/usr/bin/perl -w
  5841. ##############################################################################
  5842. #
  5843. # A simple example of converting some Unicode text to an Excel file using
  5844. # Spreadsheet::WriteExcel and perl 5.8.
  5845. #
  5846. # This example generates some Russian from a file with CP1251 encoded text.
  5847. #
  5848. #
  5849. # reverse('©'), September 2004, John McNamara, jmcnamara@cpan.org
  5850. #
  5851. # Perl 5.8 or later is required for proper utf8 handling. For older perl
  5852. # versions you should use UTF16 and the write_utf16be_string() method.
  5853. # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs.
  5854. #
  5855. require 5.008;
  5856. use strict;
  5857. use Spreadsheet::WriteExcel;
  5858. my $workbook = Spreadsheet::WriteExcel->new("unicode_cp1251.xls");
  5859. my $worksheet = $workbook->add_worksheet();
  5860. $worksheet->set_column('A:A', 50);
  5861. my $file = 'unicode_cp1251.txt';
  5862. open FH, '<:encoding(cp1251)', $file or die "Couldn't open $file: $!\n";
  5863. my $row = 0;
  5864. while (<FH>) {
  5865. next if /^#/; # Ignore the comments in the sample file.
  5866. chomp;
  5867. $worksheet->write($row++, 0, $_);
  5868. }
  5869. __END__
  5870. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/unicode_cp1251.pl>
  5871. =head2 Example: unicode_cp1256.pl
  5872. A simple example of converting some Unicode text to an Excel file using
  5873. Spreadsheet::WriteExcel and perl 5.8.
  5874. This example generates some Arabic text from a CP-1256 encoded file.
  5875. =begin html
  5876. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/unicode_cp1256.jpg" width="640" height="420" alt="Output from unicode_cp1256.pl" /></center></p>
  5877. =end html
  5878. Source code for this example:
  5879. #!/usr/bin/perl -w
  5880. ##############################################################################
  5881. #
  5882. # A simple example of converting some Unicode text to an Excel file using
  5883. # Spreadsheet::WriteExcel and perl 5.8.
  5884. #
  5885. # This example generates some Arabic text from a CP-1256 encoded file.
  5886. #
  5887. #
  5888. # reverse('©'), September 2004, John McNamara, jmcnamara@cpan.org
  5889. #
  5890. # Perl 5.8 or later is required for proper utf8 handling. For older perl
  5891. # versions you should use UTF16 and the write_utf16be_string() method.
  5892. # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs.
  5893. #
  5894. require 5.008;
  5895. use strict;
  5896. use Spreadsheet::WriteExcel;
  5897. my $workbook = Spreadsheet::WriteExcel->new("unicode_cp1256.xls");
  5898. my $worksheet = $workbook->add_worksheet();
  5899. $worksheet->set_column('A:A', 50);
  5900. my $file = 'unicode_cp1256.txt';
  5901. open FH, '<:encoding(cp1256)', $file or die "Couldn't open $file: $!\n";
  5902. my $row = 0;
  5903. while (<FH>) {
  5904. next if /^#/; # Ignore the comments in the sample file.
  5905. chomp;
  5906. $worksheet->write($row++, 0, $_);
  5907. }
  5908. __END__
  5909. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/unicode_cp1256.pl>
  5910. =head2 Example: unicode_koi8r.pl
  5911. A simple example of converting some Unicode text to an Excel file using
  5912. Spreadsheet::WriteExcel and perl 5.8.
  5913. This example generates some Russian from a file with KOI8-R encoded text.
  5914. =begin html
  5915. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/unicode_koi8r.jpg" width="640" height="420" alt="Output from unicode_koi8r.pl" /></center></p>
  5916. =end html
  5917. Source code for this example:
  5918. #!/usr/bin/perl -w
  5919. ##############################################################################
  5920. #
  5921. # A simple example of converting some Unicode text to an Excel file using
  5922. # Spreadsheet::WriteExcel and perl 5.8.
  5923. #
  5924. # This example generates some Russian from a file with KOI8-R encoded text.
  5925. #
  5926. #
  5927. # reverse('©'), September 2004, John McNamara, jmcnamara@cpan.org
  5928. #
  5929. # Perl 5.8 or later is required for proper utf8 handling. For older perl
  5930. # versions you should use UTF16 and the write_utf16be_string() method.
  5931. # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs.
  5932. #
  5933. require 5.008;
  5934. use strict;
  5935. use Spreadsheet::WriteExcel;
  5936. my $workbook = Spreadsheet::WriteExcel->new("unicode_koi8r.xls");
  5937. my $worksheet = $workbook->add_worksheet();
  5938. $worksheet->set_column('A:A', 50);
  5939. my $file = 'unicode_koi8r.txt';
  5940. open FH, '<:encoding(koi8-r)', $file or die "Couldn't open $file: $!\n";
  5941. my $row = 0;
  5942. while (<FH>) {
  5943. next if /^#/; # Ignore the comments in the sample file.
  5944. chomp;
  5945. $worksheet->write($row++, 0, $_);
  5946. }
  5947. __END__
  5948. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/unicode_koi8r.pl>
  5949. =head2 Example: unicode_polish_utf8.pl
  5950. A simple example of converting some Unicode text to an Excel file using
  5951. Spreadsheet::WriteExcel and perl 5.8.
  5952. This example generates some Polish from a file with UTF8 encoded text.
  5953. =begin html
  5954. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/unicode_polish_utf8.jpg" width="640" height="420" alt="Output from unicode_polish_utf8.pl" /></center></p>
  5955. =end html
  5956. Source code for this example:
  5957. #!/usr/bin/perl -w
  5958. ##############################################################################
  5959. #
  5960. # A simple example of converting some Unicode text to an Excel file using
  5961. # Spreadsheet::WriteExcel and perl 5.8.
  5962. #
  5963. # This example generates some Polish from a file with UTF8 encoded text.
  5964. #
  5965. #
  5966. # reverse('©'), September 2004, John McNamara, jmcnamara@cpan.org
  5967. #
  5968. # Perl 5.8 or later is required for proper utf8 handling. For older perl
  5969. # versions you should use UTF16 and the write_utf16be_string() method.
  5970. # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs.
  5971. #
  5972. require 5.008;
  5973. use strict;
  5974. use Spreadsheet::WriteExcel;
  5975. my $workbook = Spreadsheet::WriteExcel->new("unicode_polish_utf8.xls");
  5976. my $worksheet = $workbook->add_worksheet();
  5977. $worksheet->set_column('A:A', 50);
  5978. my $file = 'unicode_polish_utf8.txt';
  5979. open FH, '<:encoding(utf8)', $file or die "Couldn't open $file: $!\n";
  5980. my $row = 0;
  5981. while (<FH>) {
  5982. next if /^#/; # Ignore the comments in the sample file.
  5983. chomp;
  5984. $worksheet->write($row++, 0, $_);
  5985. }
  5986. __END__
  5987. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/unicode_polish_utf8.pl>
  5988. =head2 Example: unicode_shift_jis.pl
  5989. A simple example of converting some Unicode text to an Excel file using
  5990. Spreadsheet::WriteExcel and perl 5.8.
  5991. This example generates some Japenese text from a file with Shift-JIS
  5992. encoded text.
  5993. =begin html
  5994. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/unicode_shift_jis.jpg" width="640" height="420" alt="Output from unicode_shift_jis.pl" /></center></p>
  5995. =end html
  5996. Source code for this example:
  5997. #!/usr/bin/perl -w
  5998. ##############################################################################
  5999. #
  6000. # A simple example of converting some Unicode text to an Excel file using
  6001. # Spreadsheet::WriteExcel and perl 5.8.
  6002. #
  6003. # This example generates some Japenese text from a file with Shift-JIS
  6004. # encoded text.
  6005. #
  6006. # reverse('©'), September 2004, John McNamara, jmcnamara@cpan.org
  6007. #
  6008. # Perl 5.8 or later is required for proper utf8 handling. For older perl
  6009. # versions you should use UTF16 and the write_utf16be_string() method.
  6010. # See the write_utf16be_string section of the Spreadsheet::WriteExcel docs.
  6011. #
  6012. require 5.008;
  6013. use strict;
  6014. use Spreadsheet::WriteExcel;
  6015. my $workbook = Spreadsheet::WriteExcel->new("unicode_shift_jis.xls");
  6016. my $worksheet = $workbook->add_worksheet();
  6017. $worksheet->set_column('A:A', 50);
  6018. my $file = 'unicode_shift_jis.txt';
  6019. open FH, '<:encoding(shiftjis)', $file or die "Couldn't open $file: $!\n";
  6020. my $row = 0;
  6021. while (<FH>) {
  6022. next if /^#/; # Ignore the comments in the sample file.
  6023. chomp;
  6024. $worksheet->write($row++, 0, $_);
  6025. }
  6026. __END__
  6027. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/unicode_shift_jis.pl>
  6028. =head2 Example: csv2xls.pl
  6029. Example of how to use the WriteExcel module
  6030. Simple program to convert a CSV comma-separated value file to an Excel file.
  6031. This is more or less an non-op since Excel can read CSV files.
  6032. The program uses Text::CSV_XS to parse the CSV.
  6033. Usage: csv2xls.pl file.csv newfile.xls
  6034. NOTE: This is only a simple conversion utility for illustrative purposes.
  6035. For converting a CSV or Tab separated or any other type of delimited
  6036. text file to Excel I recommend the more rigorous csv2xls program that is
  6037. part of H.Merijn Brand's Text::CSV_XS module distro.
  6038. See the examples/csv2xls link here:
  6039. L<http://search.cpan.org/~hmbrand/Text-CSV_XS/MANIFEST>
  6040. #!/usr/bin/perl -w
  6041. ###############################################################################
  6042. #
  6043. # Example of how to use the WriteExcel module
  6044. #
  6045. # Simple program to convert a CSV comma-separated value file to an Excel file.
  6046. # This is more or less an non-op since Excel can read CSV files.
  6047. # The program uses Text::CSV_XS to parse the CSV.
  6048. #
  6049. # Usage: csv2xls.pl file.csv newfile.xls
  6050. #
  6051. #
  6052. # NOTE: This is only a simple conversion utility for illustrative purposes.
  6053. # For converting a CSV or Tab separated or any other type of delimited
  6054. # text file to Excel I recommend the more rigorous csv2xls program that is
  6055. # part of H.Merijn Brand's Text::CSV_XS module distro.
  6056. #
  6057. # See the examples/csv2xls link here:
  6058. # L<http://search.cpan.org/~hmbrand/Text-CSV_XS/MANIFEST>
  6059. #
  6060. # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org
  6061. #
  6062. use strict;
  6063. use Spreadsheet::WriteExcel;
  6064. use Text::CSV_XS;
  6065. # Check for valid number of arguments
  6066. if (($#ARGV < 1) || ($#ARGV > 2)) {
  6067. die("Usage: csv2xls csvfile.txt newfile.xls\n");
  6068. };
  6069. # Open the Comma Separated Variable file
  6070. open (CSVFILE, $ARGV[0]) or die "$ARGV[0]: $!";
  6071. # Create a new Excel workbook
  6072. my $workbook = Spreadsheet::WriteExcel->new($ARGV[1]);
  6073. my $worksheet = $workbook->add_worksheet();
  6074. # Create a new CSV parsing object
  6075. my $csv = Text::CSV_XS->new;
  6076. # Row and column are zero indexed
  6077. my $row = 0;
  6078. while (<CSVFILE>) {
  6079. if ($csv->parse($_)) {
  6080. my @Fld = $csv->fields;
  6081. my $col = 0;
  6082. foreach my $token (@Fld) {
  6083. $worksheet->write($row, $col, $token);
  6084. $col++;
  6085. }
  6086. $row++;
  6087. }
  6088. else {
  6089. my $err = $csv->error_input;
  6090. print "Text::CSV_XS parse() failed on argument: ", $err, "\n";
  6091. }
  6092. }
  6093. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/csv2xls.pl>
  6094. =head2 Example: tab2xls.pl
  6095. Example of how to use the WriteExcel module
  6096. The following converts a tab separated file into an Excel file
  6097. Usage: tab2xls.pl tabfile.txt newfile.xls
  6098. NOTE: This is only a simple conversion utility for illustrative purposes.
  6099. For converting a CSV or Tab separated or any other type of delimited
  6100. text file to Excel I recommend the more rigorous csv2xls program that is
  6101. part of H.Merijn Brand's Text::CSV_XS module distro.
  6102. See the examples/csv2xls link here:
  6103. L<http://search.cpan.org/~hmbrand/Text-CSV_XS/MANIFEST>
  6104. #!/usr/bin/perl -w
  6105. ###############################################################################
  6106. #
  6107. # Example of how to use the WriteExcel module
  6108. #
  6109. # The following converts a tab separated file into an Excel file
  6110. #
  6111. # Usage: tab2xls.pl tabfile.txt newfile.xls
  6112. #
  6113. #
  6114. # NOTE: This is only a simple conversion utility for illustrative purposes.
  6115. # For converting a CSV or Tab separated or any other type of delimited
  6116. # text file to Excel I recommend the more rigorous csv2xls program that is
  6117. # part of H.Merijn Brand's Text::CSV_XS module distro.
  6118. #
  6119. # See the examples/csv2xls link here:
  6120. # L<http://search.cpan.org/~hmbrand/Text-CSV_XS/MANIFEST>
  6121. #
  6122. # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org
  6123. #
  6124. use strict;
  6125. use Spreadsheet::WriteExcel;
  6126. # Check for valid number of arguments
  6127. if (($#ARGV < 1) || ($#ARGV > 2)) {
  6128. die("Usage: tab2xls tabfile.txt newfile.xls\n");
  6129. };
  6130. # Open the tab delimited file
  6131. open (TABFILE, $ARGV[0]) or die "$ARGV[0]: $!";
  6132. # Create a new Excel workbook
  6133. my $workbook = Spreadsheet::WriteExcel->new($ARGV[1]);
  6134. my $worksheet = $workbook->add_worksheet();
  6135. # Row and column are zero indexed
  6136. my $row = 0;
  6137. while (<TABFILE>) {
  6138. chomp;
  6139. # Split on single tab
  6140. my @Fld = split('\t', $_);
  6141. my $col = 0;
  6142. foreach my $token (@Fld) {
  6143. $worksheet->write($row, $col, $token);
  6144. $col++;
  6145. }
  6146. $row++;
  6147. }
  6148. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/tab2xls.pl>
  6149. =head2 Example: datecalc1.pl
  6150. NOTE: An easier way of writing dates and times is to use the newer
  6151. write_date_time() Worksheet method. See the date_time.pl example.
  6152. Demonstration of writing date/time cells to Excel spreadsheets,
  6153. using UNIX/Perl time as source of date/time.
  6154. UNIX/Perl time is the time since the Epoch (00:00:00 GMT, 1 Jan 1970)
  6155. measured in seconds.
  6156. An Excel file can use exactly one of two different date/time systems.
  6157. In these systems, a floating point number represents the number of days
  6158. (and fractional parts of the day) since a start point. The floating point
  6159. number is referred to as a 'serial'.
  6160. The two systems ('1900' and '1904') use different starting points:
  6161. '1900'; '1.00' is 1 Jan 1900 BUT 1900 is erroneously regarded as
  6162. a leap year - see:
  6163. http://support.microsoft.com/support/kb/articles/Q181/3/70.asp
  6164. for the excuse^H^H^H^H^H^Hreason.
  6165. '1904'; '1.00' is 2 Jan 1904.
  6166. The '1904' system is the default for Apple Macs. Windows versions of
  6167. Excel have the option to use the '1904' system.
  6168. Note that Visual Basic's "DateSerial" function does NOT erroneously
  6169. regard 1900 as a leap year, and thus its serials do not agree with
  6170. the 1900 serials of Excel for dates before 1 Mar 1900.
  6171. Note that StarOffice (at least at version 5.2) does NOT erroneously
  6172. regard 1900 as a leap year, and thus its serials do not agree with
  6173. the 1900 serials of Excel for dates before 1 Mar 1900.
  6174. #!/usr/bin/perl -w
  6175. ######################################################################
  6176. #
  6177. # NOTE: An easier way of writing dates and times is to use the newer
  6178. # write_date_time() Worksheet method. See the date_time.pl example.
  6179. #
  6180. ######################################################################
  6181. #
  6182. # Demonstration of writing date/time cells to Excel spreadsheets,
  6183. # using UNIX/Perl time as source of date/time.
  6184. #
  6185. ######################################################################
  6186. #
  6187. # UNIX/Perl time is the time since the Epoch (00:00:00 GMT, 1 Jan 1970)
  6188. # measured in seconds.
  6189. #
  6190. # An Excel file can use exactly one of two different date/time systems.
  6191. # In these systems, a floating point number represents the number of days
  6192. # (and fractional parts of the day) since a start point. The floating point
  6193. # number is referred to as a 'serial'.
  6194. #
  6195. # The two systems ('1900' and '1904') use different starting points:
  6196. #
  6197. # '1900'; '1.00' is 1 Jan 1900 BUT 1900 is erroneously regarded as
  6198. # a leap year - see:
  6199. # http://support.microsoft.com/support/kb/articles/Q181/3/70.asp
  6200. # for the excuse^H^H^H^H^H^Hreason.
  6201. # '1904'; '1.00' is 2 Jan 1904.
  6202. #
  6203. # The '1904' system is the default for Apple Macs. Windows versions of
  6204. # Excel have the option to use the '1904' system.
  6205. #
  6206. # Note that Visual Basic's "DateSerial" function does NOT erroneously
  6207. # regard 1900 as a leap year, and thus its serials do not agree with
  6208. # the 1900 serials of Excel for dates before 1 Mar 1900.
  6209. #
  6210. # Note that StarOffice (at least at version 5.2) does NOT erroneously
  6211. # regard 1900 as a leap year, and thus its serials do not agree with
  6212. # the 1900 serials of Excel for dates before 1 Mar 1900.
  6213. #
  6214. # Copyright 2000, Andrew Benham, adsb@bigfoot.com
  6215. #
  6216. ######################################################################
  6217. #
  6218. # Calculation description
  6219. # =======================
  6220. #
  6221. # 1900 system
  6222. # -----------
  6223. # Unix time is '0' at 00:00:00 GMT 1 Jan 1970, i.e. 70 years after 1 Jan 1900.
  6224. # Of those 70 years, 17 (1904,08,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68)
  6225. # were leap years with an extra day.
  6226. # Thus there were 17 + 70*365 days = 25567 days between 1 Jan 1900 and
  6227. # 1 Jan 1970.
  6228. # In the 1900 system, '1' is 1 Jan 1900, but as 1900 was not a leap year
  6229. # 1 Jan 1900 should really be '2', so 1 Jan 1970 is '25569'.
  6230. #
  6231. # 1904 system
  6232. # -----------
  6233. # Unix time is '0' at 00:00:00 GMT 1 Jan 1970, i.e. 66 years after 1 Jan 1904.
  6234. # Of those 66 years, 17 (1904,08,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68)
  6235. # were leap years with an extra day.
  6236. # Thus there were 17 + 66*365 days = 24107 days between 1 Jan 1904 and
  6237. # 1 Jan 1970.
  6238. # In the 1904 system, 2 Jan 1904 being '1', 1 Jan 1970 is '24107'.
  6239. #
  6240. ######################################################################
  6241. #
  6242. # Copyright (c) 2000, Andrew Benham.
  6243. # This program is free software. It may be used, redistributed and/or
  6244. # modified under the same terms as Perl itself.
  6245. #
  6246. # Andrew Benham, adsb@bigfoot.com
  6247. # London, United Kingdom
  6248. # 11 Nov 2000
  6249. #
  6250. ######################################################################
  6251. use strict;
  6252. use Spreadsheet::WriteExcel;
  6253. use Time::Local;
  6254. use vars qw/$DATE_SYSTEM/;
  6255. # Use 1900 date system on all platforms other than Apple Mac (for which
  6256. # use 1904 date system).
  6257. $DATE_SYSTEM = ($^O eq 'MacOS') ? 1 : 0;
  6258. my $workbook = Spreadsheet::WriteExcel->new("dates.xls");
  6259. my $worksheet = $workbook->add_worksheet();
  6260. my $format_date = $workbook->add_format();
  6261. $format_date->set_num_format('d mmmm yyy');
  6262. $worksheet->set_column(0,1,21);
  6263. $worksheet->write_string (0,0,"The epoch (GMT)");
  6264. $worksheet->write_number (0,1,&calc_serial(0,1),0x16);
  6265. $worksheet->write_string (1,0,"The epoch (localtime)");
  6266. $worksheet->write_number (1,1,&calc_serial(0,0),0x16);
  6267. $worksheet->write_string (2,0,"Today");
  6268. $worksheet->write_number (2,1,&calc_serial(),$format_date);
  6269. my $christmas2000 = timelocal(0,0,0,25,11,100);
  6270. $worksheet->write_string (3,0,"Christmas 2000");
  6271. $worksheet->write_number (3,1,&calc_serial($christmas2000),$format_date);
  6272. $workbook->close();
  6273. #-----------------------------------------------------------
  6274. # calc_serial()
  6275. #
  6276. # Called with (up to) 2 parameters.
  6277. # 1. Unix timestamp. If omitted, uses current time.
  6278. # 2. GMT flag. Set to '1' to return serial in GMT.
  6279. # If omitted, returns serial in appropriate timezone.
  6280. #
  6281. # Returns date/time serial according to $DATE_SYSTEM selected
  6282. #-----------------------------------------------------------
  6283. sub calc_serial {
  6284. my $time = (defined $_[0]) ? $_[0] : time();
  6285. my $gmtflag = (defined $_[1]) ? $_[1] : 0;
  6286. # Divide timestamp by number of seconds in a day.
  6287. # This gives a date serial with '0' on 1 Jan 1970.
  6288. my $serial = $time / 86400;
  6289. # Adjust the date serial by the offset appropriate to the
  6290. # currently selected system (1900/1904).
  6291. if ($DATE_SYSTEM == 0) { # use 1900 system
  6292. $serial += 25569;
  6293. } else { # use 1904 system
  6294. $serial += 24107;
  6295. }
  6296. unless ($gmtflag) {
  6297. # Now have a 'raw' serial with the right offset. But this
  6298. # gives a serial in GMT, which is false unless the timezone
  6299. # is GMT. We need to adjust the serial by the appropriate
  6300. # timezone offset.
  6301. # Calculate the appropriate timezone offset by seeing what
  6302. # the differences between localtime and gmtime for the given
  6303. # time are.
  6304. my @gmtime = gmtime($time);
  6305. my @ltime = localtime($time);
  6306. # For the first 7 elements of the two arrays, adjust the
  6307. # date serial where the elements differ.
  6308. for (0 .. 6) {
  6309. my $diff = $ltime[$_] - $gmtime[$_];
  6310. if ($diff) {
  6311. $serial += _adjustment($diff,$_);
  6312. }
  6313. }
  6314. }
  6315. # Perpetuate the error that 1900 was a leap year by decrementing
  6316. # the serial if we're using the 1900 system and the date is prior to
  6317. # 1 Mar 1900. This has the effect of making serial value '60'
  6318. # 29 Feb 1900.
  6319. # This fix only has any effect if UNIX/Perl time on the platform
  6320. # can represent 1900. Many can't.
  6321. unless ($DATE_SYSTEM) {
  6322. $serial-- if ($serial < 61); # '61' is 1 Mar 1900
  6323. }
  6324. return $serial;
  6325. }
  6326. sub _adjustment {
  6327. # Based on the difference in the localtime/gmtime array elements
  6328. # number, return the adjustment required to the serial.
  6329. # We only look at some elements of the localtime/gmtime arrays:
  6330. # seconds unlikely to be different as all known timezones
  6331. # have an offset of integral multiples of 15 minutes,
  6332. # but it's easy to do.
  6333. # minutes will be different for timezone offsets which are
  6334. # not an exact number of hours.
  6335. # hours very likely to be different.
  6336. # weekday will differ when localtime/gmtime difference
  6337. # straddles midnight.
  6338. #
  6339. # Assume that difference between localtime and gmtime is less than
  6340. # 5 days, then don't have to do maths for day of month, month number,
  6341. # year number, etc...
  6342. my ($delta,$element) = @_;
  6343. my $adjust = 0;
  6344. if ($element == 0) { # Seconds
  6345. $adjust = $delta/86400; # 60 * 60 * 24
  6346. } elsif ($element == 1) { # Minutes
  6347. $adjust = $delta/1440; # 60 * 24
  6348. } elsif ($element == 2) { # Hours
  6349. $adjust = $delta/24; # 24
  6350. } elsif ($element == 6) { # Day of week number
  6351. # Catch difference straddling Sat/Sun in either direction
  6352. $delta += 7 if ($delta < -4);
  6353. $delta -= 7 if ($delta > 4);
  6354. $adjust = $delta;
  6355. }
  6356. return $adjust;
  6357. }
  6358. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/datecalc1.pl>
  6359. =head2 Example: datecalc2.pl
  6360. Example of how to using the Date::Calc module to calculate Excel dates.
  6361. NOTE: An easier way of writing dates and times is to use the newer
  6362. write_date_time() Worksheet method. See the date_time.pl example.
  6363. #!/usr/bin/perl -w
  6364. ###############################################################################
  6365. #
  6366. # Example of how to using the Date::Calc module to calculate Excel dates.
  6367. #
  6368. # NOTE: An easier way of writing dates and times is to use the newer
  6369. # write_date_time() Worksheet method. See the date_time.pl example.
  6370. #
  6371. # reverse('©'), June 2001, John McNamara, jmcnamara@cpan.org
  6372. #
  6373. use strict;
  6374. use Spreadsheet::WriteExcel;
  6375. use Date::Calc qw(Delta_DHMS); # You may need to install this module.
  6376. # Create a new workbook and add a worksheet
  6377. my $workbook = Spreadsheet::WriteExcel->new("excel_date2.xls");
  6378. my $worksheet = $workbook->add_worksheet();
  6379. # Expand the first column so that the date is visible.
  6380. $worksheet->set_column("A:A", 25);
  6381. # Add a format for the date
  6382. my $format = $workbook->add_format();
  6383. $format->set_num_format('d mmmm yyy HH:MM:SS');
  6384. my $date;
  6385. # Write some dates and times
  6386. $date = excel_date(1900, 1, 1);
  6387. $worksheet->write("A1", $date, $format);
  6388. $date = excel_date(2000, 1, 1);
  6389. $worksheet->write("A2", $date, $format);
  6390. $date = excel_date(2000, 4, 17, 14, 33, 15);
  6391. $worksheet->write("A3", $date, $format);
  6392. ###############################################################################
  6393. #
  6394. # excel_date($years, $months, $days, $hours, $minutes, $seconds)
  6395. #
  6396. # Create an Excel date in the 1900 format. All of the arguments are optional
  6397. # but you should at least add $years.
  6398. #
  6399. # Corrects for Excel's missing leap day in 1900. See excel_time1.pl for an
  6400. # explanation.
  6401. #
  6402. sub excel_date {
  6403. my $years = $_[0] || 1900;
  6404. my $months = $_[1] || 1;
  6405. my $days = $_[2] || 1;
  6406. my $hours = $_[3] || 0;
  6407. my $minutes = $_[4] || 0;
  6408. my $seconds = $_[5] || 0;
  6409. my @date = ($years, $months, $days, $hours, $minutes, $seconds);
  6410. my @epoch = (1899, 12, 31, 0, 0, 0);
  6411. ($days, $hours, $minutes, $seconds) = Delta_DHMS(@epoch, @date);
  6412. my $date = $days + ($hours*3600 +$minutes*60 +$seconds)/(24*60*60);
  6413. # Add a day for Excel's missing leap day in 1900
  6414. $date++ if ($date > 59);
  6415. return $date;
  6416. }
  6417. ###############################################################################
  6418. #
  6419. # excel_date($years, $months, $days, $hours, $minutes, $seconds)
  6420. #
  6421. # Create an Excel date in the 1904 format. All of the arguments are optional
  6422. # but you should at least add $years.
  6423. #
  6424. # You will also need to call $workbook->set_1904() for this format to be valid.
  6425. #
  6426. sub excel_date_1904 {
  6427. my $years = $_[0] || 1900;
  6428. my $months = $_[1] || 1;
  6429. my $days = $_[2] || 1;
  6430. my $hours = $_[3] || 0;
  6431. my $minutes = $_[4] || 0;
  6432. my $seconds = $_[5] || 0;
  6433. my @date = ($years, $months, $days, $hours, $minutes, $seconds);
  6434. my @epoch = (1904, 1, 1, 0, 0, 0);
  6435. ($days, $hours, $minutes, $seconds) = Delta_DHMS(@epoch, @date);
  6436. my $date = $days + ($hours*3600 +$minutes*60 +$seconds)/(24*60*60);
  6437. return $date;
  6438. }
  6439. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/datecalc2.pl>
  6440. =head2 Example: lecxe.pl
  6441. Utility program to convert an Excel file into a Spreadsheet::WriteExcel
  6442. program using Win32::OLE
  6443. #!/usr/bin/perl -w
  6444. #
  6445. # Utility program to convert an Excel file into a Spreadsheet::WriteExcel
  6446. # program using Win32::OLE
  6447. #
  6448. #
  6449. # lecxe program
  6450. # by t0mas@netlords.net
  6451. #
  6452. # Version 0.01a Initial release (alpha)
  6453. # Modules
  6454. use strict;
  6455. use Win32::OLE;
  6456. use Win32::OLE::Const;
  6457. use Getopt::Std;
  6458. # Vars
  6459. use vars qw(%opts);
  6460. # Get options
  6461. getopts('i:o:v',\%opts);
  6462. # Not enough options
  6463. exit &usage unless ($opts{i} && $opts{o});
  6464. # Create Excel object
  6465. my $Excel = new Win32::OLE("Excel.Application","Quit") or
  6466. die "Can't start excel: $!";
  6467. # Get constants
  6468. my $ExcelConst=Win32::OLE::Const->Load("Microsoft Excel");
  6469. # Show Excel
  6470. $Excel->{Visible} = 1 if ($opts{v});
  6471. # Open infile
  6472. my $Workbook = $Excel->Workbooks->Open({Filename=>$opts{i}});
  6473. # Open outfile
  6474. open (OUTFILE,">$opts{o}") or die "Can't open outfile $opts{o}: $!";
  6475. # Print header for outfile
  6476. print OUTFILE <<'EOH';
  6477. #!/usr/bin/perl -w
  6478. use strict;
  6479. use Spreadsheet::WriteExcel;
  6480. use vars qw($workbook %worksheets %formats);
  6481. $workbook = Spreadsheet::WriteExcel->new("_change_me_.xls");
  6482. EOH
  6483. # Loop all sheets
  6484. foreach my $sheetnum (1..$Excel->Workbooks(1)->Worksheets->Count) {
  6485. # Format sheet
  6486. my $name=$Excel->Workbooks(1)->Worksheets($sheetnum)->Name;
  6487. print "Sheet $name\n" if ($opts{v});
  6488. print OUTFILE "# Sheet $name\n";
  6489. print OUTFILE "\$worksheets{'$name'} = \$workbook->add_worksheet('$name');\n";
  6490. # Get usedrange of cells in worksheet
  6491. my $usedrange=$Excel->Workbooks(1)->Worksheets($sheetnum)->UsedRange;
  6492. # Loop all columns in used range
  6493. foreach my $j (1..$usedrange->Columns->Count){
  6494. # Format column
  6495. print "Col $j\n" if ($opts{v});
  6496. my ($colwidth);
  6497. $colwidth=$usedrange->Columns($j)->ColumnWidth;
  6498. print OUTFILE "# Column $j\n";
  6499. print OUTFILE "\$worksheets{'$name'}->set_column(".($j-1).",".($j-1).
  6500. ", $colwidth);\n";
  6501. # Loop all rows in used range
  6502. foreach my $i (1..$usedrange->Rows->Count){
  6503. # Format row
  6504. print "Row $i\n" if ($opts{v});
  6505. print OUTFILE "# Row $i\n";
  6506. do {
  6507. my ($rowheight);
  6508. $rowheight=$usedrange->Rows($i)->RowHeight;
  6509. print OUTFILE "\$worksheets{'$name'}->set_row(".($i-1).
  6510. ", $rowheight);\n";
  6511. } if ($j==1);
  6512. # Start creating cell format
  6513. my $fname="\$formats{'".$name.'R'.$i.'C'.$j."'}";
  6514. my $format="$fname=\$workbook->add_format();\n";
  6515. my $print_format=0;
  6516. # Check for borders
  6517. my @bfnames=qw(left right top bottom);
  6518. foreach my $k (1..$usedrange->Cells($i,$j)->Borders->Count) {
  6519. my $lstyle=$usedrange->Cells($i,$j)->Borders($k)->LineStyle;
  6520. if ($lstyle > 0) {
  6521. $format.=$fname."->set_".$bfnames[$k-1]."($lstyle);\n";
  6522. $print_format=1;
  6523. }
  6524. }
  6525. # Check for font
  6526. my ($fontattr,$prop,$func,%fontsets,$fontColor);
  6527. %fontsets=(Name=>'set_font',
  6528. Size=>'set_size');
  6529. while (($prop,$func) = each %fontsets) {
  6530. $fontattr=$usedrange->Cells($i,$j)->Font->$prop;
  6531. if ($fontattr ne "") {
  6532. $format.=$fname."->$func('$fontattr');\n";
  6533. $print_format=1;
  6534. }
  6535. }
  6536. %fontsets=(Bold=>'set_bold(1)',
  6537. Italic=>'set_italic(1)',
  6538. Underline=>'set_underline(1)',
  6539. Strikethrough=>'set_strikeout(1)',
  6540. Superscript=>'set_script(1)',
  6541. Subscript=>'set_script(2)',
  6542. OutlineFont=>'set_outline(1)',
  6543. Shadow=>'set_shadow(1)');
  6544. while (($prop,$func) = each %fontsets) {
  6545. $fontattr=$usedrange->Cells($i,$j)->Font->$prop;
  6546. if ($fontattr==1) {
  6547. $format.=$fname."->$func;\n" ;
  6548. $print_format=1;
  6549. }
  6550. }
  6551. $fontColor=$usedrange->Cells($i,$j)->Font->ColorIndex();
  6552. if ($fontColor>0&&$fontColor!=$ExcelConst->{xlColorIndexAutomatic}) {
  6553. $format.=$fname."->set_color(".($fontColor+7).");\n" ;
  6554. $print_format=1;
  6555. }
  6556. # Check text alignment, merging and wrapping
  6557. my ($halign,$valign,$merge,$wrap);
  6558. $halign=$usedrange->Cells($i,$j)->HorizontalAlignment;
  6559. my %hAligns=($ExcelConst->{xlHAlignCenter}=>"'center'",
  6560. $ExcelConst->{xlHAlignJustify}=>"'justify'",
  6561. $ExcelConst->{xlHAlignLeft}=>"'left'",
  6562. $ExcelConst->{xlHAlignRight}=>"'right'",
  6563. $ExcelConst->{xlHAlignFill}=>"'fill'",
  6564. $ExcelConst->{xlHAlignCenterAcrossSelection}=>"'merge'");
  6565. if ($halign!=$ExcelConst->{xlHAlignGeneral}) {
  6566. $format.=$fname."->set_align($hAligns{$halign});\n";
  6567. $print_format=1;
  6568. }
  6569. $valign=$usedrange->Cells($i,$j)->VerticalAlignment;
  6570. my %vAligns=($ExcelConst->{xlVAlignBottom}=>"'bottom'",
  6571. $ExcelConst->{xlVAlignCenter}=>"'vcenter'",
  6572. $ExcelConst->{xlVAlignJustify}=>"'vjustify'",
  6573. $ExcelConst->{xlVAlignTop}=>"'top'");
  6574. if ($valign) {
  6575. $format.=$fname."->set_align($vAligns{$valign});\n";
  6576. $print_format=1;
  6577. }
  6578. $merge=$usedrange->Cells($i,$j)->MergeCells;
  6579. if ($merge==1) {
  6580. $format.=$fname."->set_merge();\n";
  6581. $print_format=1;
  6582. }
  6583. $wrap=$usedrange->Cells($i,$j)->WrapText;
  6584. if ($wrap==1) {
  6585. $format.=$fname."->set_text_wrap(1);\n";
  6586. $print_format=1;
  6587. }
  6588. # Check patterns
  6589. my ($pattern,%pats);
  6590. %pats=(-4142=>0,-4125=>2,-4126=>3,-4124=>4,-4128=>5,-4166=>6,
  6591. -4121=>7,-4162=>8);
  6592. $pattern=$usedrange->Cells($i,$j)->Interior->Pattern;
  6593. if ($pattern&&$pattern!=$ExcelConst->{xlPatternAutomatic}) {
  6594. $pattern=$pats{$pattern} if ($pattern<0 && defined $pats{$pattern});
  6595. $format.=$fname."->set_pattern($pattern);\n";
  6596. # Colors fg/bg
  6597. my ($cIndex);
  6598. $cIndex=$usedrange->Cells($i,$j)->Interior->PatternColorIndex;
  6599. if ($cIndex>0&&$cIndex!=$ExcelConst->{xlColorIndexAutomatic}) {
  6600. $format.=$fname."->set_bg_color(".($cIndex+7).");\n";
  6601. }
  6602. $cIndex=$usedrange->Cells($i,$j)->Interior->ColorIndex;
  6603. if ($cIndex>0&&$cIndex!=$ExcelConst->{xlColorIndexAutomatic}) {
  6604. $format.=$fname."->set_fg_color(".($cIndex+7).");\n";
  6605. }
  6606. $print_format=1;
  6607. }
  6608. # Check for number format
  6609. my ($num_format);
  6610. $num_format=$usedrange->Cells($i,$j)->NumberFormat;
  6611. if ($num_format ne "") {
  6612. $format.=$fname."->set_num_format('$num_format');\n";
  6613. $print_format=1;
  6614. }
  6615. # Check for contents (text or formula)
  6616. my ($contents);
  6617. $contents=$usedrange->Cells($i,$j)->Formula;
  6618. $contents=$usedrange->Cells($i,$j)->Text if ($contents eq "");
  6619. # Print cell
  6620. if ($contents ne "" or $print_format) {
  6621. print OUTFILE "# Cell($i,$j)\n";
  6622. print OUTFILE $format if ($print_format);
  6623. print OUTFILE "\$worksheets{'$name'}->write(".($i-1).",".($j-1).
  6624. ",'$contents'";
  6625. print OUTFILE ",$fname" if ($print_format);
  6626. print OUTFILE ");\n";
  6627. }
  6628. }
  6629. }
  6630. }
  6631. # Famous last words...
  6632. print OUTFILE "\$workbook->close();\n";
  6633. # Close outfile
  6634. close (OUTFILE) or die "Can't close outfile $opts{o}: $!";
  6635. ####################################################################
  6636. sub usage {
  6637. printf STDERR "usage: $0 [options]\n".
  6638. "\tOptions:\n".
  6639. "\t\t-v \tverbose mode\n" .
  6640. "\t\t-i <name>\tname of input file\n" .
  6641. "\t\t-o <name>\tname of output file\n";
  6642. }
  6643. ####################################################################
  6644. sub END {
  6645. # Quit excel
  6646. do {
  6647. $Excel->{DisplayAlerts} = 0;
  6648. $Excel->Quit;
  6649. } if (defined $Excel);
  6650. }
  6651. __END__
  6652. =head1 NAME
  6653. lecxe - A Excel file to Spreadsheet::WriteExcel code converter
  6654. =head1 DESCRIPTION
  6655. This program takes an MS Excel workbook file as input and from
  6656. that file, produces an output file with Perl code that uses the
  6657. Spreadsheet::WriteExcel module to reproduce the original
  6658. file.
  6659. =head1 STUFF
  6660. Additional hands-on editing of the output file might be neccecary
  6661. as:
  6662. * This program always names the file produced by output script
  6663. _change_me_.xls
  6664. * Users of international Excel versions will have som work to do
  6665. on list separators and numeric punctation characters.
  6666. =head1 SEE ALSO
  6667. L<Win32::OLE>, L<Win32::OLE::Variant>, L<Spreadsheet::WriteExcel>
  6668. =head1 BUGS
  6669. * Picks wrong color on cells sometimes.
  6670. * Probably a few other...
  6671. =head1 DISCLAIMER
  6672. I do not guarantee B<ANYTHING> with this program. If you use it you
  6673. are doing so B<AT YOUR OWN RISK>! I may or may not support this
  6674. depending on my time schedule...
  6675. =head1 AUTHOR
  6676. t0mas@netlords.net
  6677. =head1 COPYRIGHT
  6678. Copyright 2001, t0mas@netlords.net
  6679. This package is free software; you can redistribute it and/or
  6680. modify it under the same terms as Perl itself.
  6681. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/lecxe.pl>
  6682. =head2 Example: convertA1.pl
  6683. This program contains helper functions to deal with the Excel A1 cell
  6684. reference notation.
  6685. These functions have been superseded by L<Spreadsheet::WriteExcel::Utility>.
  6686. #!/usr/bin/perl -w
  6687. ###############################################################################
  6688. #
  6689. # This program contains helper functions to deal with the Excel A1 cell
  6690. # reference notation.
  6691. #
  6692. # These functions have been superseded by L<Spreadsheet::WriteExcel::Utility>.
  6693. #
  6694. # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org
  6695. #
  6696. use strict;
  6697. print "\n";
  6698. print "Cell B7 is equivalent to (";
  6699. print join " ", cell_to_rowcol('B7');
  6700. print ") in row column notation.\n";
  6701. print "Cell \$B7 is equivalent to (";
  6702. print join " ", cell_to_rowcol('$B7');
  6703. print ") in row column notation.\n";
  6704. print "Cell B\$7 is equivalent to (";
  6705. print join " ", cell_to_rowcol('B$7');
  6706. print ") in row column notation.\n";
  6707. print "Cell \$B\$7 is equivalent to (";
  6708. print join " ", cell_to_rowcol('$B$7');
  6709. print ") in row column notation.\n\n";
  6710. print "Row and column (1999, 29) are equivalent to ";
  6711. print rowcol_to_cell(1999, 29), ".\n";
  6712. print "Row and column (1999, 29, 0, 1) are equivalent to ";
  6713. print rowcol_to_cell(1999, 29, 0, 1), ".\n\n";
  6714. print "The base cell is: Z7\n";
  6715. print "Increment the row: ", inc_cell_row('Z7'), "\n";
  6716. print "Decrement the row: ", dec_cell_row('Z7'), "\n";
  6717. print "Increment the column: ", inc_cell_col('Z7'), "\n";
  6718. print "Decrement the column: ", dec_cell_col('Z7'), "\n\n";
  6719. ###############################################################################
  6720. #
  6721. # rowcol_to_cell($row, $col, $row_absolute, $col_absolute)
  6722. #
  6723. # Convert a zero based row and column reference to a A1 reference. For example
  6724. # (0, 2) to C1. $row_absolute, $col_absolute are optional. They are boolean
  6725. # values used to indicate if the row or column value is absolute, i.e. if it is
  6726. # prefixed by a $ sign: eg. (0, 2, 0, 1) converts to $C1.
  6727. #
  6728. # Returns: a cell reference string.
  6729. #
  6730. sub rowcol_to_cell {
  6731. my $row = $_[0];
  6732. my $col = $_[1];
  6733. my $row_abs = $_[2] || 0;
  6734. my $col_abs = $_[3] || 0;
  6735. if ($row_abs) {
  6736. $row_abs = '$'
  6737. }
  6738. else {
  6739. $row_abs = ''
  6740. }
  6741. if ($col_abs) {
  6742. $col_abs = '$'
  6743. }
  6744. else {
  6745. $col_abs = ''
  6746. }
  6747. my $int = int ($col / 26);
  6748. my $frac = $col % 26 +1;
  6749. my $chr1 ='';
  6750. my $chr2 ='';
  6751. if ($frac != 0) {
  6752. $chr2 = chr (ord('A') + $frac -1);;
  6753. }
  6754. if ($int > 0) {
  6755. $chr1 = chr (ord('A') + $int -1);
  6756. }
  6757. $row++; # Zero index to 1-index
  6758. return $col_abs . $chr1 . $chr2 . $row_abs. $row;
  6759. }
  6760. ###############################################################################
  6761. #
  6762. # cell_to_rowcol($cell_ref)
  6763. #
  6764. # Convert an Excel cell reference in A1 notation to a zero based row and column
  6765. # reference; converts C1 to (0, 2, 0, 0).
  6766. #
  6767. # Returns: row, column, row_is_absolute, column_is_absolute
  6768. #
  6769. #
  6770. sub cell_to_rowcol {
  6771. my $cell = shift;
  6772. $cell =~ /(\$?)([A-I]?[A-Z])(\$?)(\d+)/;
  6773. my $col_abs = $1 eq "" ? 0 : 1;
  6774. my $col = $2;
  6775. my $row_abs = $3 eq "" ? 0 : 1;
  6776. my $row = $4;
  6777. # Convert base26 column string to number
  6778. # All your Base are belong to us.
  6779. my @chars = split //, $col;
  6780. my $expn = 0;
  6781. $col = 0;
  6782. while (@chars) {
  6783. my $char = pop(@chars); # LS char first
  6784. $col += (ord($char) -ord('A') +1) * (26**$expn);
  6785. $expn++;
  6786. }
  6787. # Convert 1-index to zero-index
  6788. $row--;
  6789. $col--;
  6790. return $row, $col, $row_abs, $col_abs;
  6791. }
  6792. ###############################################################################
  6793. #
  6794. # inc_cell_row($cell_ref)
  6795. #
  6796. # Increments the row number of an Excel cell reference in A1 notation.
  6797. # For example C3 to C4
  6798. #
  6799. # Returns: a cell reference string.
  6800. #
  6801. sub inc_cell_row {
  6802. my $cell = shift;
  6803. my ($row, $col, $row_abs, $col_abs) = cell_to_rowcol($cell);
  6804. $row++;
  6805. return rowcol_to_cell($row, $col, $row_abs, $col_abs);
  6806. }
  6807. ###############################################################################
  6808. #
  6809. # dec_cell_row($cell_ref)
  6810. #
  6811. # Decrements the row number of an Excel cell reference in A1 notation.
  6812. # For example C4 to C3
  6813. #
  6814. # Returns: a cell reference string.
  6815. #
  6816. sub dec_cell_row {
  6817. my $cell = shift;
  6818. my ($row, $col, $row_abs, $col_abs) = cell_to_rowcol($cell);
  6819. $row--;
  6820. return rowcol_to_cell($row, $col, $row_abs, $col_abs);
  6821. }
  6822. ###############################################################################
  6823. #
  6824. # inc_cell_col($cell_ref)
  6825. #
  6826. # Increments the column number of an Excel cell reference in A1 notation.
  6827. # For example C3 to D3
  6828. #
  6829. # Returns: a cell reference string.
  6830. #
  6831. sub inc_cell_col {
  6832. my $cell = shift;
  6833. my ($row, $col, $row_abs, $col_abs) = cell_to_rowcol($cell);
  6834. $col++;
  6835. return rowcol_to_cell($row, $col, $row_abs, $col_abs);
  6836. }
  6837. ###############################################################################
  6838. #
  6839. # dec_cell_col($cell_ref)
  6840. #
  6841. # Decrements the column number of an Excel cell reference in A1 notation.
  6842. # For example D3 to C3
  6843. #
  6844. # Returns: a cell reference string.
  6845. #
  6846. sub dec_cell_col {
  6847. my $cell = shift;
  6848. my ($row, $col, $row_abs, $col_abs) = cell_to_rowcol($cell);
  6849. $col--;
  6850. return rowcol_to_cell($row, $col, $row_abs, $col_abs);
  6851. }
  6852. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/convertA1.pl>
  6853. =head2 Example: function_locale.pl
  6854. Generate function names for different locales.
  6855. This program can be used to generate the hash of known functions for use in
  6856. the Formula.pm module. By default the function names are in English but you
  6857. can also choose to support the following languages: German, French, Spanish,
  6858. Portuguese, Dutch, Finnish, Italian and Swedish.
  6859. This would allow you to do something like the following:
  6860. $worksheet->write(0, 0, '=SUM(C1:C3)' );
  6861. $worksheet->write(1, 0, '=SUMME(C1:C3)');
  6862. $worksheet->write(2, 0, '=SOMME(C1:C3)');
  6863. $worksheet->write(3, 0, '=SUMA(C1:C3)' );
  6864. $worksheet->write(4, 0, '=SOMA(C1:C3)' );
  6865. $worksheet->write(5, 0, '=SOM(C1:C3)' );
  6866. $worksheet->write(6, 0, '=SUMMA(C1:C3)');
  6867. $worksheet->write(7, 0, '=SOMMA(C1:C3)');
  6868. Unfortunately, if you wish to support more than one language there are some
  6869. conflicts between function names:
  6870. Function Language 1 Language 2
  6871. ======== ========== ==========
  6872. NB French Dutch
  6873. NA English/French Finnish
  6874. TRIM French English
  6875. DIA Spanish/Portuguese German
  6876. Therefore, if you try to generate a hash of function names to support both
  6877. French and English then the function TRIM will be assigned the meaning of the
  6878. first language that defines it, which in this case is French. You can get
  6879. around this by renaming the function for one of the languages and documenting
  6880. the change, for example: TRIM.EN or TRIM.FR.
  6881. Please note that this only partially solves the problem of localisation.
  6882. There are also number formats to consider (1.5 == 1,5) and the fact that the
  6883. list separator "," and the array separator ";" are interchanged in different
  6884. locales.
  6885. The longest function name is LOI.NORMALE.STANDARD.INVERSE (29 chars) followed
  6886. by NORM.JAKAUMA.NORMIT.KÄÄNT (25 chars).
  6887. The shortest function name in all languages is T.
  6888. #!/usr/bin/perl -w
  6889. ###############################################################################
  6890. #
  6891. # Generate function names for different locales.
  6892. #
  6893. # This program can be used to generate the hash of known functions for use in
  6894. # the Formula.pm module. By default the function names are in English but you
  6895. # can also choose to support the following languages: German, French, Spanish,
  6896. # Portuguese, Dutch, Finnish, Italian and Swedish.
  6897. #
  6898. # This would allow you to do something like the following:
  6899. #
  6900. # $worksheet->write(0, 0, '=SUM(C1:C3)' );
  6901. # $worksheet->write(1, 0, '=SUMME(C1:C3)');
  6902. # $worksheet->write(2, 0, '=SOMME(C1:C3)');
  6903. # $worksheet->write(3, 0, '=SUMA(C1:C3)' );
  6904. # $worksheet->write(4, 0, '=SOMA(C1:C3)' );
  6905. # $worksheet->write(5, 0, '=SOM(C1:C3)' );
  6906. # $worksheet->write(6, 0, '=SUMMA(C1:C3)');
  6907. # $worksheet->write(7, 0, '=SOMMA(C1:C3)');
  6908. #
  6909. # Unfortunately, if you wish to support more than one language there are some
  6910. # conflicts between function names:
  6911. #
  6912. # Function Language 1 Language 2
  6913. # ======== ========== ==========
  6914. # NB French Dutch
  6915. # NA English/French Finnish
  6916. # TRIM French English
  6917. # DIA Spanish/Portuguese German
  6918. #
  6919. # Therefore, if you try to generate a hash of function names to support both
  6920. # French and English then the function TRIM will be assigned the meaning of the
  6921. # first language that defines it, which in this case is French. You can get
  6922. # around this by renaming the function for one of the languages and documenting
  6923. # the change, for example: TRIM.EN or TRIM.FR.
  6924. #
  6925. # Please note that this only partially solves the problem of localisation.
  6926. # There are also number formats to consider (1.5 == 1,5) and the fact that the
  6927. # list separator "," and the array separator ";" are interchanged in different
  6928. # locales.
  6929. #
  6930. # The longest function name is LOI.NORMALE.STANDARD.INVERSE (29 chars) followed
  6931. # by NORM.JAKAUMA.NORMIT.KÄÄNT (25 chars).
  6932. # The shortest function name in all languages is T.
  6933. #
  6934. #
  6935. # reverse('©'); John McNamara, March 2001, jmcnamara@cpan.org
  6936. #
  6937. use strict;
  6938. #
  6939. # Modify the following variables to add the language to the function name hash
  6940. #
  6941. my $english = 1;
  6942. my $german = 0;
  6943. my $french = 0;
  6944. my $spanish = 0;
  6945. my $portuguese = 0;
  6946. my $dutch = 0;
  6947. my $finnish = 0;
  6948. my $italian = 0;
  6949. my $swedish = 0;
  6950. my %funcs;
  6951. # Ignore the headings
  6952. <DATA>;
  6953. # Print the beginning of the hash definition
  6954. print " %functions = (\n";
  6955. print " #" . " " x 37 ;
  6956. print "ptg args class vol\n";
  6957. while (<DATA>){
  6958. my @F = split " ";
  6959. my $value = $F[0];
  6960. my $args = $F[1];
  6961. my $ref = $F[2];
  6962. my $vol = $F[3];
  6963. print_function($F[4], $value, $args, $ref, $vol) if $english;
  6964. print_function($F[5], $value, $args, $ref, $vol) if $german;
  6965. print_function($F[6], $value, $args, $ref, $vol) if $french;
  6966. print_function($F[7], $value, $args, $ref, $vol) if $spanish;
  6967. print_function($F[8], $value, $args, $ref, $vol) if $portuguese;
  6968. print_function($F[9], $value, $args, $ref, $vol) if $dutch;
  6969. print_function($F[10], $value, $args, $ref, $vol) if $finnish;
  6970. print_function($F[11], $value, $args, $ref, $vol) if $italian;
  6971. print_function($F[12], $value, $args, $ref, $vol) if $swedish;
  6972. }
  6973. # Print the end of the hash definition
  6974. print " );\n";
  6975. ###############################################################################
  6976. #
  6977. # Function to print the function names. It prints a warning if there is a
  6978. # clash.
  6979. #
  6980. sub print_function {
  6981. my $func = shift;
  6982. my $value = shift;
  6983. my $args = shift;
  6984. my $ref = shift;
  6985. my $vol = shift;
  6986. $func = "'$func'";
  6987. if (not exists $funcs{$func}) {
  6988. printf(" %-31s => [%4d, %4d, %4d, %4d ],\n",
  6989. $func, $value, $args, $ref, $vol);
  6990. $funcs{$func} = $value;
  6991. }
  6992. else {
  6993. if ($funcs{$func} != $value) {
  6994. print " # Warning ";
  6995. print $func, " is already defined in another language\n";
  6996. printf(" #%-31s => [%4d, %4d, %4d, %4d ],\n",
  6997. $func, $value, $args, $ref, $vol);
  6998. }
  6999. }
  7000. }
  7001. # Note: The following data contains the function names in the various
  7002. # languages. These lines are LONG.
  7003. __DATA__
  7004. Value Args Refclass Volatile English German French Spanish Portuguese Dutch Finnish Italian Swedish
  7005. 0 -1 0 0 COUNT ANZAHL NB CONTAR CONT.NÚM AANTAL LASKE CONTA.NUMERI ANTAL
  7006. 1 -1 1 0 IF WENN SI SI SE ALS JOS SE OM
  7007. 2 1 1 0 ISNA ISTNV ESTNA ESNOD É.NÃO.DISP ISNB ONPUUTTUU VAL.NON.DISP ÄRSAKNAD
  7008. 3 1 1 0 ISERROR ISTFEHLER ESTERREUR ESERROR ÉERROS ISFOUT ONVIRHE VAL.ERRORE ÄRFEL
  7009. 4 -1 0 0 SUM SUMME SOMME SUMA SOMA SOM SUMMA SOMMA SUMMA
  7010. 5 -1 0 0 AVERAGE MITTELWERT MOYENNE PROMEDIO MÉDIA GEMIDDELDE KESKIARVO MEDIA MEDEL
  7011. 6 -1 0 0 MIN MIN MIN MIN MÍNIMO MIN MIN MIN MIN
  7012. 7 -1 0 0 MAX MAX MAX MAX MÁXIMO MAX MAKS MAX MAX
  7013. 8 -1 0 0 ROW ZEILE LIGNE FILA LIN RIJ RIVI RIF.RIGA RAD
  7014. 9 -1 0 0 COLUMN SPALTE COLONNE COLUMNA COL KOLOM SARAKE RIF.COLONNA KOLUMN
  7015. 10 0 0 0 NA NV NA NOD NÃO.DISP NB PUUTTUU NON.DISP SAKNAS
  7016. 11 -1 1 0 NPV NBW VAN VNA VPL NHW NNA VAN NETNUVÄRDE
  7017. 12 -1 0 0 STDEV STABW ECARTYPE DESVEST DESVPAD STDEV KESKIHAJONTA DEV.ST STDAV
  7018. 13 -1 1 0 DOLLAR DM FRANC MONEDA MOEDA GULDEN VALUUTTA VALUTA VALUTA
  7019. 14 -1 1 0 FIXED FEST CTXT DECIMAL DEF.NÚM.DEC VAST KIINTEÄ FISSO FASTTAL
  7020. 15 1 1 0 SIN SIN SIN SENO SEN SIN SIN SEN SIN
  7021. 16 1 1 0 COS COS COS COS COS COS COS COS COS
  7022. 17 1 1 0 TAN TAN TAN TAN TAN TAN TAN TAN TAN
  7023. 18 1 1 0 ATAN ARCTAN ATAN ATAN ATAN BOOGTAN ATAN ARCTAN ARCTAN
  7024. 19 0 1 0 PI PI PI PI PI PI PII PI.GRECO PI
  7025. 20 1 1 0 SQRT WURZEL RACINE RAIZ RAIZ WORTEL NELIÖJUURI RADQ ROT
  7026. 21 1 1 0 EXP EXP EXP EXP EXP EXP EKSPONENTTI EXP EXP
  7027. 22 1 1 0 LN LN LN LN LN LN LUONNLOG LN LN
  7028. 23 1 1 0 LOG10 LOG10 LOG10 LOG10 LOG10 LOG10 LOG10 LOG10 LOG10
  7029. 24 1 1 0 ABS ABS ABS ABS ABS ABS ITSEISARVO ASS ABS
  7030. 25 1 1 0 INT GANZZAHL ENT ENTERO INT INTEGER KOKONAISLUKU INT HELTAL
  7031. 26 1 1 0 SIGN VORZEICHEN SIGNE SIGNO SINAL POS.NEG ETUMERKKI SEGNO TECKEN
  7032. 27 2 1 0 ROUND RUNDEN ARRONDI REDONDEAR ARRED AFRONDEN PYÖRISTÄ ARROTONDA AVRUNDA
  7033. 28 -1 0 0 LOOKUP VERWEIS RECHERCHE BUSCAR PROC ZOEKEN HAKU CERCA LETAUPP
  7034. 29 -1 0 1 INDEX INDEX INDEX INDICE ÍNDICE INDEX INDEKSI INDICE INDEX
  7035. 30 2 1 0 REPT WIEDERHOLEN REPT REPETIR REPETIR HERHALING TOISTA RIPETI REP
  7036. 31 3 1 0 MID TEIL STXT EXTRAE EXT.TEXTO DEEL POIMI.TEKSTI STRINGA.ESTRAI EXTEXT
  7037. 32 1 1 0 LEN LÄNGE NBCAR LARGO NÚM.CARACT LENGTE PITUUS LUNGHEZZA LÄNGD
  7038. 33 1 1 0 VALUE WERT CNUM VALOR VALOR WAARDE ARVO VALORE TEXTNUM
  7039. 34 0 1 0 TRUE WAHR VRAI VERDADERO VERDADEIRO WAAR TOSI VERO SANT
  7040. 35 0 1 0 FALSE FALSCH FAUX FALSO FALSO ONWAAR EPÄTOSI FALSO FALSKT
  7041. 36 -1 0 0 AND UND ET Y E EN JA E OCH
  7042. 37 -1 0 0 OR ODER OU O OU OF TAI O ELLER
  7043. 38 1 1 0 NOT NICHT NON NO NÃO NIET EI NON ICKE
  7044. 39 2 1 0 MOD REST MOD RESIDUO MOD REST JAKOJ RESTO REST
  7045. 40 3 0 0 DCOUNT DBANZAHL BDNB BDCONTAR BDCONTAR DBAANTAL TLASKE DB.CONTA.NUMERI DANTAL
  7046. 41 3 0 0 DSUM DBSUMME BDSOMME BDSUMA BDSOMA DBSOM TSUMMA DB.SOMMA DSUMMA
  7047. 42 3 0 0 DAVERAGE DBMITTELWERT BDMOYENNE BDPROMEDIO BDMÉDIA DBGEMIDDELDE TKESKIARVO DB.MEDIA DMEDEL
  7048. 43 3 0 0 DMIN DBMIN BDMIN BDMIN BDMÍN DBMIN TMIN DB.MIN DMIN
  7049. 44 3 0 0 DMAX DBMAX BDMAX BDMAX BDMÁX DBMAX TMAKS DB.MAX DMAX
  7050. 45 3 0 0 DSTDEV DBSTDABW BDECARTYPE BDDESVEST BDEST DBSTDEV TKESKIHAJONTA DB.DEV.ST DSTDAV
  7051. 46 -1 0 0 VAR VARIANZ VAR VAR VAR VAR VAR VAR VARIANS
  7052. 47 3 0 0 DVAR DBVARIANZ BDVAR BDVAR BDVAREST DBVAR TVARIANSSI DB.VAR DVARIANS
  7053. 48 2 1 0 TEXT TEXT TEXTE TEXTO TEXTO TEKST TEKSTI TESTO TEXT
  7054. 49 -1 0 0 LINEST RGP DROITEREG ESTIMACION.LINEAL PROJ.LIN LIJNSCH LINREGR REGR.LIN REGR
  7055. 50 -1 0 0 TREND TREND TENDANCE TENDENCIA TENDÊNCIA TREND SUUNTAUS TENDENZA TREND
  7056. 51 -1 0 0 LOGEST RKP LOGREG ESTIMACION.LOGARITMICA PROJ.LOG LOGSCH LOGREGR REGR.LOG EXPREGR
  7057. 52 -1 0 0 GROWTH VARIATION CROISSANCE CRECIMIENTO CRESCIMENTO GROEI KASVU CRESCITA EXPTREND
  7058. 56 -1 1 0 PV BW VA VA VP HW NA VA NUVÄRDE
  7059. 57 -1 1 0 FV ZW VC VF VF TW TULEVA.ARVO VAL.FUT SLUTVÄRDE
  7060. 58 -1 1 0 NPER ZZR NPM NPER NPER NPER NJAKSO NUM.RATE PERIODER
  7061. 59 -1 1 0 PMT RMZ VPM PAGO PGTO BET MAKSU RATA BETALNING
  7062. 60 -1 1 0 RATE ZINS TAUX TASA TAXA RENTE KORKO TASSO RÄNTA
  7063. 61 3 0 0 MIRR QIKV TRIM TIRM MTIR GIR MSISÄINEN TIR.VAR MODIR
  7064. 62 -1 0 0 IRR IKV TRI TIR TIR IR SISÄINEN.KORKO TIR.COST IR
  7065. 63 0 1 1 RAND ZUFALLSZAHL ALEA ALEATORIO ALEATÓRIO ASELECT SATUNNAISLUKU CASUALE SLUMP
  7066. 64 -1 0 0 MATCH VERGLEICH EQUIV COINCIDIR CORRESP VERGELIJKEN VASTINE CONFRONTA PASSA
  7067. 65 3 1 0 DATE DATUM DATE FECHA DATA DATUM PÄIVÄYS DATA DATUM
  7068. 66 3 1 0 TIME ZEIT TEMPS NSHORA TEMPO TIJD AIKA ORARIO KLOCKSLAG
  7069. 67 1 1 0 DAY TAG JOUR DIA DIA DAG PÄIVÄ GIORNO DAG
  7070. 68 1 1 0 MONTH MONAT MOIS MES MÊS MAAND KUUKAUSI MESE MÅNAD
  7071. 69 1 1 0 YEAR JAHR ANNEE AÑO ANO JAAR VUOSI ANNO ÅR
  7072. 70 -1 1 0 WEEKDAY WOCHENTAG JOURSEM DIASEM DIA.DA.SEMANA WEEKDAG VIIKONPÄIVÄ GIORNO.SETTIMANA VECKODAG
  7073. 71 1 1 0 HOUR STUNDE HEURE HORA HORA UUR TUNNIT ORA TIMME
  7074. 72 1 1 0 MINUTE MINUTE MINUTE MINUTO MINUTO MINUUT MINUUTIT MINUTO MINUT
  7075. 73 1 1 0 SECOND SEKUNDE SECONDE SEGUNDO SEGUNDO SECONDE SEKUNNIT SECONDO SEKUND
  7076. 74 0 1 1 NOW JETZT MAINTENANT AHORA AGORA NU NYT ADESSO NU
  7077. 75 1 0 1 AREAS BEREICHE ZONES AREAS ÁREAS BEREIKEN ALUEET AREE OMRÅDEN
  7078. 76 1 0 1 ROWS ZEILEN LIGNES FILAS LINS RIJEN RIVIT RIGHE RADER
  7079. 77 1 0 1 COLUMNS SPALTEN COLONNES COLUMNAS COLS KOLOMMEN SARAKKEET COLONNE KOLUMNER
  7080. 78 -1 0 1 OFFSET BEREICH.VERSCHIEBEN DECALER DESREF DESLOC VERSCHUIVING SIIRTYMÄ SCARTO FÖRSKJUTNING
  7081. 82 -1 1 0 SEARCH SUCHEN CHERCHE HALLAR LOCALIZAR VIND.SPEC KÄY.LÄPI RICERCA SÖK
  7082. 83 1 1 0 TRANSPOSE MTRANS TRANSPOSE TRANSPONER TRANSPOR TRANSPONEREN TRANSPONOI MATR.TRASPOSTA TRANSPONERA
  7083. 86 1 1 0 TYPE TYP TYPE TIPO TIPO TYPE TYYPPI TIPO VÄRDETYP
  7084. 97 2 1 0 ATAN2 ARCTAN2 ATAN2 ATAN2 ATAN2 BOOGTAN2 ATAN2 ARCTAN.2 ARCTAN2
  7085. 98 1 1 0 ASIN ARCSIN ASIN ASENO ASEN BOOGSIN ASIN ARCSEN ARCSIN
  7086. 99 1 1 0 ACOS ARCCOS ACOS ACOS ACOS BOOGCOS ACOS ARCCOS ARCCOS
  7087. 100 -1 1 0 CHOOSE WAHL CHOISIR ELEGIR ESCOLHER KIEZEN VALITSE.INDEKSI SCEGLI VÄLJ
  7088. 101 -1 0 0 HLOOKUP WVERWEIS RECHERCHEH BUSCARH PROCH HORIZ.ZOEKEN VHAKU CERCA.ORIZZ LETAKOLUMN
  7089. 102 -1 0 0 VLOOKUP SVERWEIS RECHERCHEV BUSCARV PROCV VERT.ZOEKEN PHAKU CERCA.VERT LETARAD
  7090. 105 1 0 0 ISREF ISTBEZUG ESTREF ESREF ÉREF ISVERWIJZING ONVIITT VAL.RIF ÄRREF
  7091. 109 -1 1 0 LOG LOG LOG LOG LOG LOG LOG LOG LOG
  7092. 111 1 1 0 CHAR ZEICHEN CAR CARACTER CARACT TEKEN MERKKI CODICE.CARATT TECKENKOD
  7093. 112 1 1 0 LOWER KLEIN MINUSCULE MINUSC MINÚSCULA KLEINE.LETTERS PIENET MINUSC GEMENER
  7094. 113 1 1 0 UPPER GROSS MAJUSCULE MAYUSC MAIÚSCULA HOOFDLETTERS ISOT MAIUSC VERSALER
  7095. 114 1 1 0 PROPER GROSS2 NOMPROPRE NOMPROPIO PRI.MAIÚSCULA BEGINLETTERS ERISNIMI MAIUSC.INIZ INITIAL
  7096. 115 -1 1 0 LEFT LINKS GAUCHE IZQUIERDA ESQUERDA LINKS VASEN SINISTRA VÄNSTER
  7097. 116 -1 1 0 RIGHT RECHTS DROITE DERECHA DIREITA RECHTS OIKEA DESTRA HÖGER
  7098. 117 2 1 0 EXACT IDENTISCH EXACT IGUAL EXATO GELIJK VERTAA IDENTICO EXAKT
  7099. 118 1 1 0 TRIM GLÄTTEN SUPPRESPACE ESPACIOS ARRUMAR SPATIES.WISSEN POISTA.VÄLIT ANNULLA.SPAZI RENSA
  7100. 119 4 1 0 REPLACE ERSETZEN REMPLACER REEMPLAZAR MUDAR VERVANGEN KORVAA RIMPIAZZA ERSÄTT
  7101. 120 -1 1 0 SUBSTITUTE WECHSELN SUBSTITUE SUSTITUIR SUBSTITUIR SUBSTITUEREN VAIHDA SOSTITUISCI BYT.UT
  7102. 121 1 1 0 CODE CODE CODE CODIGO CÓDIGO CODE KOODI CODICE KOD
  7103. 124 -1 1 0 FIND FINDEN TROUVE ENCONTRAR PROCURAR VIND.ALLES ETSI TROVA HITTA
  7104. 125 -1 0 1 CELL ZELLE CELLULE CELDA CÉL CEL SOLU CELLA CELL
  7105. 126 1 1 0 ISERR ISTFEHL ESTERR ESERR ÉERRO ISFOUT2 ONVIRH VAL.ERR ÄRF
  7106. 127 1 1 0 ISTEXT ISTTEXT ESTTEXTE ESTEXTO ÉTEXTO ISTEKST ONTEKSTI VAL.TESTO ÄRTEXT
  7107. 128 1 1 0 ISNUMBER ISTZAHL ESTNUM ESNUMERO ÉNÚM ISGETAL ONLUKU VAL.NUMERO ÄRTAL
  7108. 129 1 1 0 ISBLANK ISTLEER ESTVIDE ESBLANCO ÉCÉL.VAZIA ISLEEG ONTYHJÄ VAL.VUOTO ÄRTOM
  7109. 130 1 0 0 T T T T T T T T T
  7110. 131 1 0 0 N N N N N N N NUM N
  7111. 140 1 1 0 DATEVALUE DATWERT DATEVAL FECHANUMERO DATA.VALOR DATUMWAARDE PÄIVÄYSARVO DATA.VALORE DATUMVÄRDE
  7112. 141 1 1 0 TIMEVALUE ZEITWERT TEMPSVAL HORANUMERO VALOR.TEMPO TIJDWAARDE AIKA_ARVO ORARIO.VALORE TIDVÄRDE
  7113. 142 3 1 0 SLN LIA AMORLIN SLN DPD LIN.AFSCHR STP AMMORT.COST LINAVSKR
  7114. 143 4 1 0 SYD DIA SYD SYD SDA SYD VUOSIPOISTO AMMORT.ANNUO ÅRSAVSKR
  7115. 144 -1 1 0 DDB GDA DDB DDB BDD DDB DDB AMMORT DEGAVSKR
  7116. 148 -1 1 1 INDIRECT INDIREKT INDIRECT INDIRECTO INDIRETO INDIRECT EPÄSUORA INDIRETTO INDIREKT
  7117. 150 -1 1 0 CALL AUFRUFEN FONCTION.APPELANTE LLAMAR CHAMAR ROEPEN KUTSU RICHIAMA ANROPA
  7118. 162 1 1 0 CLEAN SÄUBERN EPURAGE LIMPIAR TIRAR WISSEN.CONTROL SIIVOA LIBERA STÄDA
  7119. 163 1 2 0 MDETERM MDET DETERMAT MDETERM MATRIZ.DETERM DETERMINANTMAT MDETERM MATR.DETERM MDETERM
  7120. 164 1 2 0 MINVERSE MINV INVERSEMAT MINVERSA MATRIZ.INVERSO INVERSEMAT MKÄÄNTEINEN MATR.INVERSA MINVERT
  7121. 165 2 2 0 MMULT MMULT PRODUITMAT MMULT MATRIZ.MULT PRODUKTMAT MKERRO MATR.PRODOTTO MMULT
  7122. 167 -1 1 0 IPMT ZINSZ INTPER PAGOINT IPGTO IBET IPMT INTERESSI RBETALNING
  7123. 168 -1 1 0 PPMT KAPZ PRINCPER PAGOPRIN PPGTO PBET PPMT P.RATA AMORT
  7124. 169 -1 0 0 COUNTA ANZAHL2 NBVAL CONTARA CONT.VALORES AANTALARG LASKE.A CONTA.VALORI ANTALV
  7125. 183 -1 0 0 PRODUCT PRODUKT PRODUIT PRODUCTO MULT PRODUKT TULO PRODOTTO PRODUKT
  7126. 184 1 1 0 FACT FAKULTÄT FACT FACT FATORIAL FACULTEIT KERTOMA FATTORIALE FAKULTET
  7127. 189 3 0 0 DPRODUCT DBPRODUKT BDPRODUIT BDPRODUCTO BDMULTIPL DBPRODUKT TTULO DB.PRODOTTO DPRODUKT
  7128. 190 1 1 0 ISNONTEXT ISTKTEXT ESTNONTEXTE ESNOTEXTO É.NÃO.TEXTO ISGEENTEKST ONEI_TEKSTI VAL.NON.TESTO ÄREJTEXT
  7129. 193 -1 0 0 STDEVP STABWN ECARTYPEP DESVESTP DESVPADP STDEVP KESKIHAJONTAP DEV.ST.POP STDAVP
  7130. 194 -1 0 0 VARP VARIANZEN VAR.P VARP VARP VARP VARP VAR.POP VARIANSP
  7131. 195 3 0 0 DSTDEVP DBSTDABWN BDECARTYPEP BDDESVESTP BDDESVPA DBSTDEVP TKESKIHAJONTAP DB.DEV.ST.POP DSTDAVP
  7132. 196 3 0 0 DVARP DBVARIANZEN BDVARP BDVARP BDVARP DBVARP TVARIANSSIP DB.VAR.POP DVARIANSP
  7133. 197 -1 1 0 TRUNC KÜRZEN TRONQUE TRUNCAR TRUNCAR GEHEEL KATKAISE TRONCA AVKORTA
  7134. 198 1 1 0 ISLOGICAL ISTLOG ESTLOGIQUE ESLOGICO ÉLÓGICO ISLOGISCH ONTOTUUS VAL.LOGICO ÄRLOGISK
  7135. 199 3 0 0 DCOUNTA DBANZAHL2 BDNBVAL BDCONTARA BDCONTARA DBAANTALC TLASKEA DB.CONTA.VALORI DANTALV
  7136. 212 2 1 0 ROUNDUP AUFRUNDEN ARRONDI.SUP REDONDEAR.MAS ARREDONDAR.PARA.CIMA AFRONDEN.NAAR.BOVEN PYÖRISTÄ.DES.YLÖS ARROTONDA.PER.ECC AVRUNDA.UPPÅT
  7137. 213 2 1 0 ROUNDDOWN ABRUNDEN ARRONDI.INF REDONDEAR.MENOS ARREDONDAR.PARA.BAIXO AFRONDEN.NAAR.BENEDEN PYÖRISTÄ.DES.ALAS ARROTONDA.PER.DIF AVRUNDA.NEDÅT
  7138. 216 -1 0 0 RANK RANG RANG JERARQUIA ORDEM RANG ARVON.MUKAAN RANGO RANG
  7139. 219 -1 1 0 ADDRESS ADRESSE ADRESSE DIRECCION ENDEREÇO ADRES OSOITE INDIRIZZO ADRESS
  7140. 220 -1 1 0 DAYS360 TAGE360 JOURS360 DIAS360 DIAS360 DAGEN360 PÄIVÄT360 GIORNO360 DAGAR360
  7141. 221 0 1 1 TODAY HEUTE AUJOURDHUI HOY HOJE VANDAAG TÄMÄ.PÄIVÄ OGGI IDAG
  7142. 222 -1 1 0 VDB VDB VDB DVS BDV VDB VDB AMMORT.VAR VDEGRAVSKR
  7143. 227 -1 0 0 MEDIAN MEDIAN MEDIANE MEDIANA MED MEDIAAN MEDIAANI MEDIANA MEDIAN
  7144. 228 -1 2 0 SUMPRODUCT SUMMENPRODUKT SOMMEPROD SUMAPRODUCTO SOMARPRODUTO SOMPRODUKT TULOJEN.SUMMA MATR.SOMMA.PRODOTTO PRODUKTSUMMA
  7145. 229 1 1 0 SINH SINHYP SINH SENOH SENH SINH SINH SENH SINH
  7146. 230 1 1 0 COSH COSHYP COSH COSH COSH COSH COSH COSH COSH
  7147. 231 1 1 0 TANH TANHYP TANH TANH TANH TANH TANH TANH TANH
  7148. 232 1 1 0 ASINH ARCSINHYP ASINH ASENOH ASENH BOOGSINH ASINH ARCSENH ARCSINH
  7149. 233 1 1 0 ACOSH ARCCOSHYP ACOSH ACOSH ACOSH BOOGCOSH ACOSH ARCCOSH ARCCOSH
  7150. 234 1 1 0 ATANH ARCTANHYP ATANH ATANH ATANH BOOGTANH ATANH ARCTANH ARCTANH
  7151. 235 3 0 0 DGET DBAUSZUG BDLIRE BDEXTRAER BDEXTRAIR DBLEZEN TNOUDA DB.VALORI DHÄMTA
  7152. 244 1 1 1 INFO INFO INFO INFO INFORMAÇÃO INFO KUVAUS AMBIENTE.INFO INFO
  7153. 247 -1 1 0 DB GDA2 DB DB BD DB DB AMMORT.FISSO DB
  7154. 252 2 0 0 FREQUENCY HÄUFIGKEIT FREQUENCE FRECUENCIA FREQÜÊNCIA INTERVAL TAAJUUS FREQUENZA FREKVENS
  7155. 261 1 1 0 ERROR.TYPE FEHLER.TYP TYPE.ERREUR TIPO.DE.ERROR TIPO.ERRO TYPE.FOUT VIRHEEN.LAJI ERRORE.TIPO FEL.TYP
  7156. 267 -1 1 0 REGISTER.ID REGISTER.KENNUMMER REGISTRE.NUMERO ID.REGISTRO IDENT.REGISTRO REGISTRATIE.ID REKISTERI.TUNNUS IDENTIFICATORE.REGISTRO REGISTRERA.ID
  7157. 269 -1 0 0 AVEDEV MITTELABW ECART.MOYEN DESVPROM DESV.MÉDIO GEM.DEVIATIE KESKIPOIKKEAMA MEDIA.DEV MEDELAVV
  7158. 270 -1 1 0 BETADIST BETAVERT LOI.BETA DISTR.BETA DISTBETA BETA.VERD BEETAJAKAUMA DISTRIB.BETA BETAFÖRD
  7159. 271 1 1 0 GAMMALN GAMMALN LNGAMMA GAMMA.LN LNGAMA GAMMA.LN GAMMALN LN.GAMMA GAMMALN
  7160. 272 -1 1 0 BETAINV BETAINV BETA.INVERSE DISTR.BETA.INV BETA.ACUM.INV BETA.INV BEETAJAKAUMA.KÄÄNT INV.BETA BETAINV
  7161. 273 4 1 0 BINOMDIST BINOMVERT LOI.BINOMIALE DISTR.BINOM DISTRBINOM BINOMIALE.VERD BINOMIJAKAUMA DISTRIB.BINOM BINOMFÖRD
  7162. 274 2 1 0 CHIDIST CHIVERT LOI.KHIDEUX DISTR.CHI DIST.QUI CHI.KWADRAAT CHIJAKAUMA DISTRIB.CHI CHI2FÖRD
  7163. 275 2 1 0 CHIINV CHIINV KHIDEUX.INVERSE PRUEBA.CHI.INV INV.QUI CHI.KWADRAAT.INV CHIJAKAUMA.KÄÄNT INV.CHI CHI2INV
  7164. 276 2 1 0 COMBIN KOMBINATIONEN COMBIN COMBINAT COMBIN COMBINATIES KOMBINAATIO COMBINAZIONE KOMBIN
  7165. 277 3 1 0 CONFIDENCE KONFIDENZ INTERVALLE.CONFIANCE INTERVALO.CONFIANZA INT.CONFIANÇA BETROUWBAARHEID LUOTTAMUSVÄLI CONFIDENZA KONFIDENS
  7166. 278 3 1 0 CRITBINOM KRITBINOM CRITERE.LOI.BINOMIALE BINOM.CRIT CRIT.BINOM CRIT.BINOM BINOMIJAKAUMA.KRIT CRIT.BINOM KRITBINOM
  7167. 279 1 1 0 EVEN GERADE PAIR REDONDEA.PAR PAR EVEN PARILLINEN PARI JÄMN
  7168. 280 3 1 0 EXPONDIST EXPONVERT LOI.EXPONENTIELLE DISTR.EXP DISTEXPON EXPON.VERD EKSPONENTIAALIJAKAUMA DISTRIB.EXP EXPONFÖRD
  7169. 281 3 1 0 FDIST FVERT LOI.F DISTR.F DISTF F.VERDELING FJAKAUMA DISTRIB.F FFÖRD
  7170. 282 3 1 0 FINV FINV INVERSE.LOI.F DISTR.F.INV INVF F.INVERSE FJAKAUMA.KÄÄNT INV.F FINV
  7171. 283 1 1 0 FISHER FISHER FISHER FISHER FISHER FISHER FISHER FISHER FISHER
  7172. 284 1 1 0 FISHERINV FISHERINV FISHER.INVERSE PRUEBA.FISHER.INV FISHERINV FISHER.INV FISHER.KÄÄNT INV.FISHER FISHERINV
  7173. 285 2 1 0 FLOOR UNTERGRENZE PLANCHER MULTIPLO.INFERIOR ARREDMULTB AFRONDEN.BENEDEN PYÖRISTÄ.KERR.ALAS ARROTONDA.DIFETTO RUNDA.NER
  7174. 286 4 1 0 GAMMADIST GAMMAVERT LOI.GAMMA DISTR.GAMMA DISTGAMA GAMMA.VERD GAMMAJAKAUMA DISTRIB.GAMMA GAMMAFÖRD
  7175. 287 3 1 0 GAMMAINV GAMMAINV LOI.GAMMA.INVERSE DISTR.GAMMA.INV INVGAMA GAMMA.INV GAMMAJAKAUMA.KÄÄNT INV.GAMMA GAMMAINV
  7176. 288 2 1 0 CEILING OBERGRENZE PLAFOND MULTIPLO.SUPERIOR TETO AFRONDEN.BOVEN PYÖRISTÄ.KERR.YLÖS ARROTONDA.ECCESSO RUNDA.UPP
  7177. 289 4 1 0 HYPGEOMDIST HYPGEOMVERT LOI.HYPERGEOMETRIQUE DISTR.HIPERGEOM DIST.HIPERGEOM HYPERGEO.VERD HYPERGEOM.JAKAUMA DISTRIB.IPERGEOM HYPGEOMFÖRD
  7178. 290 3 1 0 LOGNORMDIST LOGNORMVERT LOI.LOGNORMALE DISTR.LOG.NORM DIST.LOGNORMAL LOG.NORM.VERD LOGNORM.JAKAUMA DISTRIB.LOGNORM LOGNORMFÖRD
  7179. 291 3 1 0 LOGINV LOGINV LOI.LOGNORMALE.INVERSE DISTR.LOG.INV INVLOG LOG.NORM.INV LOGNORM.JAKAUMA.KÄÄNT INV.LOGNORM LOGINV
  7180. 292 3 1 0 NEGBINOMDIST NEGBINOMVERT LOI.BINOMIALE.NEG NEGBINOMDIST DIST.BIN.NEG NEG.BINOM.VERD BINOMIJAKAUMA.NEG DISTRIB.BINOM.NEG NEGBINOMFÖRD
  7181. 293 4 1 0 NORMDIST NORMVERT LOI.NORMALE DISTR.NORM DIST.NORM NORM.VERD NORM.JAKAUMA DISTRIB.NORM NORMFÖRD
  7182. 294 1 1 0 NORMSDIST STANDNORMVERT LOI.NORMALE.STANDARD DISTR.NORM.ESTAND DIST.NORMP STAND.NORM.VERD NORM.JAKAUMA.NORMIT DISTRIB.NORM.ST NORMSFÖRD
  7183. 295 3 1 0 NORMINV NORMINV LOI.NORMALE.INVERSE DISTR.NORM.INV INV.NORM NORM.INV NORM.JAKAUMA.KÄÄNT INV.NORM NORMINV
  7184. 296 1 1 0 NORMSINV STANDNORMINV LOI.NORMALE.STANDARD.INVERSE DISTR.NORM.ESTAND.INV INV.NORMP STAND.NORM.INV NORM.JAKAUMA.NORMIT.KÄÄNT INV.NORM.ST NORMSINV
  7185. 297 3 1 0 STANDARDIZE STANDARDISIERUNG CENTREE.REDUITE NORMALIZACION PADRONIZAR NORMALISEREN NORMITA NORMALIZZA STANDARDISERA
  7186. 298 1 1 0 ODD UNGERADE IMPAIR REDONDEA.IMPAR ÍMPAR ONEVEN PARITON DISPARI UDDA
  7187. 299 2 1 0 PERMUT VARIATIONEN PERMUTATION PERMUTACIONES PERMUT PERMUTATIES PERMUTAATIO PERMUTAZIONE PERMUT
  7188. 300 3 1 0 POISSON POISSON LOI.POISSON POISSON POISSON POISSON POISSON POISSON POISSON
  7189. 301 3 1 0 TDIST TVERT LOI.STUDENT DISTR.T DISTT T.VERD TJAKAUMA DISTRIB.T TFÖRD
  7190. 302 4 1 0 WEIBULL WEIBULL LOI.WEIBULL DIST.WEIBULL WEIBULL WEIBULL WEIBULL WEIBULL WEIBULL
  7191. 303 2 2 0 SUMXMY2 SUMMEXMY2 SOMME.XMY2 SUMAXMENOSY2 SOMAXMY2 SOM.XMINY.2 EROTUSTEN.NELIÖSUMMA SOMMA.Q.DIFF SUMMAXMY2
  7192. 304 2 2 0 SUMX2MY2 SUMMEX2MY2 SOMME.X2MY2 SUMAX2MENOSY2 SOMAX2DY2 SOM.X2MINY2 NELIÖSUMMIEN.EROTUS SOMMA.DIFF.Q SUMMAX2MY2
  7193. 305 2 2 0 SUMX2PY2 SUMMEX2PY2 SOMME.X2PY2 SUMAX2MASY2 SOMAX2SY2 SOM.X2PLUSY2 NELIÖSUMMIEN.SUMMA SOMMA.SOMMA.Q SUMMAX2PY2
  7194. 306 2 2 0 CHITEST CHITEST TEST.KHIDEUX PRUEBA.CHI TESTE.QUI CHI.TOETS CHITESTI TEST.CHI CHI2TEST
  7195. 307 2 2 0 CORREL KORREL COEFFICIENT.CORRELATION COEF.DE.CORREL CORREL CORRELATIE KORRELAATIO CORRELAZIONE KORREL
  7196. 308 2 2 0 COVAR KOVAR COVARIANCE COVAR COVAR COVARIANTIE KOVARIANSSI COVARIANZA KOVAR
  7197. 309 3 2 0 FORECAST SCHÄTZER PREVISION PRONOSTICO PREVISÃO VOORSPELLEN ENNUSTE PREVISIONE PREDIKTION
  7198. 310 2 2 0 FTEST FTEST TEST.F PRUEBA.F TESTEF F.TOETS FTESTI TEST.F FTEST
  7199. 311 2 2 0 INTERCEPT ACHSENABSCHNITT ORDONNEE.ORIGINE INTERSECCION INTERCEPÇÃO SNIJPUNT LEIKKAUSPISTE INTERCETTA SKÄRNINGSPUNKT
  7200. 312 2 2 0 PEARSON PEARSON PEARSON PEARSON PEARSON PEARSON PEARSON PEARSON PEARSON
  7201. 313 2 2 0 RSQ BESTIMMTHEITSMASS COEFFICIENT.DETERMINATION COEFICIENTE.R2 RQUAD R.KWADRAAT PEARSON.NELIÖ RQ RKV
  7202. 314 2 2 0 STEYX STFEHLERYX ERREUR.TYPE.XY ERROR.TIPICO.XY EPADYX STAND.FOUT.YX KESKIVIRHE ERR.STD.YX STDFELYX
  7203. 315 2 2 0 SLOPE STEIGUNG PENTE PENDIENTE INCLINAÇÃO RICHTING KULMAKERROIN PENDENZA LUTNING
  7204. 316 4 2 0 TTEST TTEST TEST.STUDENT PRUEBA.T TESTET T.TOETS TTESTI TEST.T TTEST
  7205. 317 -1 2 0 PROB WAHRSCHBEREICH PROBABILITE PROBABILIDAD PROB KANS TODENNÄKÖISYYS PROBABILITÀ SANNOLIKHET
  7206. 318 -1 0 0 DEVSQ SUMQUADABW SOMME.CARRES.ECARTS DESVIA2 DESVQ DEV.KWAD OIKAISTU.NELIÖSUMMA DEV.Q KVADAVV
  7207. 319 -1 0 0 GEOMEAN GEOMITTEL MOYENNE.GEOMETRIQUE MEDIA.GEOM MÉDIA.GEOMÉTRICA MEETK.GEM KESKIARVO.GEOM MEDIA.GEOMETRICA GEOMEDEL
  7208. 320 -1 0 0 HARMEAN HARMITTEL MOYENNE.HARMONIQUE MEDIA.ARMO MÉDIA.HARMÔNICA HARM.GEM KESKIARVO.HARM MEDIA.ARMONICA HARMMEDEL
  7209. 321 -1 0 0 SUMSQ QUADRATESUMME SOMME.CARRES SUMA.CUADRADOS SOMAQUAD KWADRATENSOM NELIÖSUMMA SOMMA.Q KVADRATSUMMA
  7210. 322 -1 0 0 KURT KURT KURTOSIS CURTOSIS CURT KURTOSIS KURT CURTOSI TOPPIGHET
  7211. 323 -1 0 0 SKEW SCHIEFE COEFFICIENT.ASYMETRIE COEFICIENTE.ASIMETRIA DISTORÇÃO SCHEEFHEID JAKAUMAN.VINOUS ASIMMETRIA SNEDHET
  7212. 324 -1 0 0 ZTEST GTEST TEST.Z PRUEBA.Z TESTEZ Z.TOETS ZTESTI TEST.Z ZTEST
  7213. 325 2 0 0 LARGE KGRÖSSTE GRANDE.VALEUR K.ESIMO.MAYOR MAIOR GROOTSTE SUURI GRANDE STÖRSTA
  7214. 326 2 0 0 SMALL KKLEINSTE PETITE.VALEUR K.ESIMO.MENOR MENOR KLEINSTE PIENI PICCOLO MINSTA
  7215. 327 2 0 0 QUARTILE QUARTILE QUARTILE CUARTIL QUARTIL KWARTIEL NELJÄNNES QUARTILE KVARTIL
  7216. 328 2 0 0 PERCENTILE QUANTIL CENTILE PERCENTIL PERCENTIL PERCENTIEL PROSENTTIPISTE PERCENTILE PERCENTIL
  7217. 329 -1 0 0 PERCENTRANK QUANTILSRANG RANG.POURCENTAGE RANGO.PERCENTIL ORDEM.PORCENTUAL PERCENT.RANG PROSENTTIJÄRJESTYS PERCENT.RANGO PROCENTRANG
  7218. 330 -1 2 0 MODE MODALWERT MODE MODA MODO MODUS MOODI MODA TYPVÄRDE
  7219. 331 2 0 0 TRIMMEAN GESTUTZTMITTEL MOYENNE.REDUITE MEDIA.ACOTADA MÉDIA.INTERNA GETRIMD.GEM KESKIARVO.TASATTU MEDIA.TRONCATA TRIMMEDEL
  7220. 332 2 1 0 TINV TINV LOI.STUDENT.INVERSE DISTR.T.INV INVT T.INV TJAKAUMA.KÄÄNT INV.T TINV
  7221. 336 -1 1 0 CONCATENATE VERKETTEN CONCATENER CONCATENAR CONCATENAR TEKST.SAMENVOEGEN KETJUTA CONCATENA SAMMANFOGA
  7222. 337 2 1 0 POWER POTENZ PUISSANCE POTENCIA POTÊNCIA MACHT POTENSSI POTENZA UPPHÖJT.TILL
  7223. 342 1 1 0 RADIANS RADIANT RADIANS RADIANES RADIANOS RADIALEN RADIAANIT RADIANTI RADIANER
  7224. 343 1 1 0 DEGREES GRAD DEGRES GRADOS GRAUS GRADEN ASTEET GRADI GRADER
  7225. 344 -1 0 0 SUBTOTAL TEILERGEBNIS SOUS.TOTAL SUBTOTALES SUBTOTAL SUBTOTAAL VÄLISUMMA SUBTOTALE DELSUMMA
  7226. 345 -1 0 0 SUMIF SUMMEWENN SOMME.SI SUMAR.SI SOMASE SOM.ALS SUMMA.JOS SOMMA.SE SUMMA.OM
  7227. 346 2 0 0 COUNTIF ZÄHLENWENN NB.SI CONTAR.SI CONT.SE AANTAL.ALS LASKE.JOS CONTA.SE ANTAL.OM
  7228. 347 1 0 0 COUNTBLANK ANZAHLLEEREZELLEN NB.VIDE CONTAR.BLANCO CONTAR.VAZIO AANTAL.LEGE.CELLEN LASKE.TYHJÄT CONTA.VUOTE ANTAL.TOMMA
  7229. 354 -1 1 0 ROMAN RÖMISCH ROMAIN NUMERO.ROMANO ROMANO ROMEINS ROMAN ROMANO ROMERSK
  7230. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/function_locale.pl>
  7231. =head2 Example: writeA1.pl
  7232. This is an example of how to extend the Spreadsheet::WriteExcel module.
  7233. Code is appended to the Spreadsheet::WriteExcel::Worksheet module by reusing
  7234. the package name. The new code provides a write() method that allows you to
  7235. use Excels A1 style cell references. This is not particularly useful but it
  7236. serves as an example of how the module can be extended without modifying the
  7237. code directly.
  7238. #!/usr/bin/perl -w
  7239. ###############################################################################
  7240. #
  7241. # This is an example of how to extend the Spreadsheet::WriteExcel module.
  7242. #
  7243. # Code is appended to the Spreadsheet::WriteExcel::Worksheet module by reusing
  7244. # the package name. The new code provides a write() method that allows you to
  7245. # use Excels A1 style cell references. This is not particularly useful but it
  7246. # serves as an example of how the module can be extended without modifying the
  7247. # code directly.
  7248. #
  7249. # reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org
  7250. #
  7251. use strict;
  7252. use Spreadsheet::WriteExcel;
  7253. # Create a new workbook called simple.xls and add a worksheet
  7254. my $workbook = Spreadsheet::WriteExcel->new("writeA1.xls");
  7255. my $worksheet = $workbook->add_worksheet();
  7256. # Write numbers or text
  7257. $worksheet->write (0, 0, "Hello");
  7258. $worksheet->writeA1("A3", "A3" );
  7259. $worksheet->writeA1("A5", 1.2345 );
  7260. ###############################################################################
  7261. #
  7262. # The following will be appended to the Spreadsheet::WriteExcel::Worksheet
  7263. # package.
  7264. #
  7265. package Spreadsheet::WriteExcel::Worksheet;
  7266. ###############################################################################
  7267. #
  7268. # writeA1($cell, $token, $format)
  7269. #
  7270. # Convert $cell from Excel A1 notation to $row, $col notation and
  7271. # call write() on $token.
  7272. #
  7273. # Returns: return value of called subroutine or -4 for invalid cell
  7274. # reference.
  7275. #
  7276. sub writeA1 {
  7277. my $self = shift;
  7278. my $cell = shift;
  7279. my $col;
  7280. my $row;
  7281. if ($cell =~ /([A-z]+)(\d+)/) {
  7282. ($row, $col) = _convertA1($2, $1);
  7283. $self->write($row, $col, @_);
  7284. } else {
  7285. return -4;
  7286. }
  7287. }
  7288. ###############################################################################
  7289. #
  7290. # _convertA1($row, $col)
  7291. #
  7292. # Convert Excel A1 notation to $row, $col notation. Convert base26 column
  7293. # string to a number.
  7294. #
  7295. sub _convertA1 {
  7296. my $row = $_[0];
  7297. my $col = $_[1]; # String in AA notation
  7298. my @chars = split //, $col;
  7299. my $expn = 0;
  7300. $col = 0;
  7301. while (@chars) {
  7302. my $char = uc(pop(@chars)); # LS char first
  7303. $col += (ord($char) -ord('A') +1) * (26**$expn);
  7304. $expn++;
  7305. }
  7306. # Convert 1 index to 0 index
  7307. $row--;
  7308. $col--;
  7309. return($row, $col);
  7310. }
  7311. Download this example: L<http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-2.37/examples/writeA1.pl>
  7312. =head1 AUTHOR
  7313. John McNamara jmcnamara@cpan.org
  7314. Contributed examples contain the original author's name.
  7315. =head1 COPYRIGHT
  7316. Copyright MM-MMX, John McNamara.
  7317. All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.
  7318. =cut