㈠ poi生成word后word里面的图片不变形
你好,你是问poi生成word后如何保证word里面的图片不变形吗?
1、确保生成的图片尽可能地与最终需要插入的Word文档保持相同的比例。可以通过设置图片大小或缩放来实现。
2、在将图片插入到Word文档之前,最好在一个图片编辑软件中打开并进行调整,以便将其完全适配Word页面并按照正确的比例显示。
3、推荐使用POI中提供的setAutoSize(true)方法自动调整页面布局大小,这样可以确保文本和图片都能够均匀分布且没有间隔。
4、在将图片插入到Word文档中时,可以选择“不锁定纵横比”选项搭清,以确保图片在Word文档中不变形。poi是基于OfficeOpenXML标准(OOXML)和Microsoft的OLE2复液扰合文档格式(OLE2)处理各种文件知埋前格式的开源项目。
㈡ 请教:如何用JAVA POI 向Word文档里插图片 请提供下代码 谢谢!!!
楼主你可以这样操作,导入java.io.*。org.apache.poi.hwpf.*。org.apache.poi.hwpf.usermodel.Picture
String savePath= "c:\\temp\\";
String docFile= savePath+ "test.doc";
String imgFile= savePath+ "img.jpg";
HWPFDocument poiDoc = new HWPFDocument(new FileInputStream(docFile));
List picList=poiDoc.getPicturesTable().getAllPictures();
Picture picture=(Picture)picList.get(0);
try {
picture.writeImageContent(new FileOutputStream(imgFile));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
㈢ 镐庝箞镙风敤Poi澶勭悊鍗曚釜镄刢ell
链绡囬溃钖戝硅薄涓筼ava镄勫埯瀛﹁咃纴浠庝笅杞藉繀澶囱蒋浠跺埌鍒涘缓涓涓绌虹槠镄凯OI宸ョ▼锛屽凡缁忕啛缁冩帉鎻$幆澧冩惌寤虹殑璇疯烦杩囨ゆ枃銆
寮鍙戠幆澧冧负windowsXP-SP2锛孍clipse3.2锛孞DK1.5.07锛屽叾瀹炲樊涓嶅氶兘琛岋纴涓崭细链夊お澶х殑宸寮伞傛湰鏂囦粠POI涓嬭浇寮濮嬭茶В锛屽墠鎻愭槸寮鍙戠幆澧冨寘𨰾绯荤粺鍙橀噺绛夌瓑宸茬粡姝g‘璁剧疆瀹屾瘯銆
1.POI镄勪笅杞
鎴镊虫湰鏂囧彂琛锛孭OI链鏂扮増链鏄3.0.1锛岃窛绂讳笂涓涓狥inal鐗堟湰链3骞翠简钖э纴浠ヨ呖浜庢垜宸镣瑰効灏辨妸浠栨斁寮冧简钬斺斾互涓烘病浜虹′简銆傚畼鏂圭殑鍏锻婅诧纴杩欎竴鐗堜富瑕佹槸杩藉姞浜嗕竴浜涙柊镄勭壒镐у拰BUG淇姝o纴涓嶈繃绋嶅井链夌偣鍎块仐镋剧殑鏄锛岃缮涓嶈兘寰埚ソ镄勬搷浣淓xcel2007銆
POI瀹樻柟缃戝潃锛
•
JAR鍖呬笅杞斤细
•
婧愮爜涓嬭浇锛
•
涓嬭浇涓婇溃镄凧AR鍜屾簮镰侊纸婧愮爜涓嶆槸蹇呴’镄勶纴浣嗘槸涓轰简镞╂棩淇寰楁f灉锛屾繁鍏ョ郴缁熺殑鐪嬬湅POI浠g爜杩樻槸链夊繀瑕佺殑锛夊苟瑙e帇寰呯敤銆
2.鐢‥clipse鎼寤洪”鐩
• 镓揿紑Eclipse
•渚濇$偣鍑籉ile->New->Java Project
•杈揿叆椤圭洰钖岖О锛屾湰渚嬩腑璁剧疆涓篜OI
•鍗曞嚮瀹屾垚
•鍦ㄩ”鐩涓婄偣鍑诲彸阌->New->Folder
•杈揿叆鏂囦欢澶瑰悕绉发ib
•鎶婂垰镓嶈В铡嬬殑poi-3.0.1-FINAL-20070705.jar澶嶅埗杩囨潵
•鍙抽敭镣瑰嚮椤圭洰锛岄夋嫨Properties
•鍦ㄥ乏渚у垪琛ㄩ噷阃変腑Java Build Path锛屽彸渚ч変腑Libraries
•镣瑰嚮Add JARs锛岄夋嫨POI椤圭洰镄刲ib涓嬬殑镓链夋枃浠
•涓ゆOK纭璁わ纴锲炲埌Eclipse鐣岄溃
灏忔妧宸э纴蹇鎹锋搷浣滐细鍙浠ョ敤榧犳爣宸﹂敭阃変腑poi-3.0.1-FINAL-20070705.jar浣嗕笉𨱒惧紑锛屾嫋鍒颁换锷℃爮镄凟clipse锲炬爣涓婄瓑鍊1绉掑乏鍙筹纴Eclipse浼氲嚜锷ㄥ脊璧锋潵锛屼緷铹朵笉𨱒惧紑绉诲姩鍒发ib鏂囦欢澶逛笂锛岃繖涓镞跺欓紶镙囧悗闱㈣窡涓鍗佸瓧绗﹀彿锛屾涧寮宸﹂敭锛屽氨瀹屾垚浜嗗嶅埗锷ㄤ綔銆傝繖涓鏄瀵规暣涓犸indows绯荤粺閮藉ソ鐢ㄧ殑蹇鎹峰嶅埗鏂瑰纺锛岃嗘簮鐩樼﹀拰鐩镙囩洏绗︾殑涓嶅悓锅跺皵浼氱敤鍒癈trl阌銆
鍒版や负姝锛屾垜浠锅氩ソ浜哖OI瀛︿範镄勫墠鎻愬嗳澶囷纴鎺ヤ笅𨱒ュ皢浠庢渶绠鍗旷殑鏂囨。鍒涘缓寮濮嬩竴姝ヤ竴姝ュ︿範镐庢牱璁㏄OI镟村ソ镄勪负鎴戜滑宸ヤ綔銆
绗涓璁 锛氩熀链镄凟xcel璇诲啓
链鏂囦富瑕佹紨绀轰竴涓婸OI镄勫熀链镎崭綔锛屼緥濡傛庢牱璇诲彇鍜屽垱寤轰竴涓鍏蜂綋镄凟xcel鏂囦欢銆傛寜镦ф傥渚嬶纴𨰾縃elloWorld璇翠簨鍎裤
璇存槑锛氭湰鏂囦富瑕佸唴瀹圭殕鍖呭惈浜庡畼鏂瑰府锷╂坠鍐岋纴涔嬫墍浠ヨ佹嬁鍑烘潵锛屽嚭浜庝袱涓铡熷洜锛屾坠鍐屾槸鑻辨枃镄+镓嫔唽鏄瀵瑰簲2.5.1镄勚
镙稿绩浠g爜濡备笅锛屾敞閲婇儴鍒嗕负璁茶В銆 杩欓噷鍙鎸戝共镄勮诧纴瀹屾暣镄勪唬镰佽峰弬钥(涓嬭浇浠g爜)銆
//鍒涘缓涓涓绌虹槠镄刉orkBook
HSSFWorkbook wb = new HSSFWorkbook();
//锘轰簬涓婇溃镄刉orkBook鍒涘缓灞炰簬姝WorkBook镄凷heet锛
//3.0.1鐗埚湪浣跨敤鍏ㄨ扴heet钖岖殑镞跺欎笉蹇呭啀setEncdoing浜嗭纴涓浜烘劅瑙夋柟渚夸简璁稿氥
HSSFSheet st = wb.createSheet("娴嬭瘯椤");
//鍒涘缓灞炰簬涓婇溃Sheet镄凴ow锛屽弬鏁0鍙浠ユ槸0锝65535涔嬮棿镄勪换浣曚竴涓锛
//娉ㄦ剰锛屽敖绠″弬鏁版槸Int绫诲瀷锛屼絾鏄疎xcel链澶氭敮鎸65536琛
HSSFRow row = st.createRow(0);
//鍒涘缓灞炰簬涓婇溃Row镄凛ell锛屽弬鏁0鍙浠ユ槸0锝255涔嬮棿镄勪换浣曚竴涓锛
//钖屾牱锛屾槸锲犱负Excel链澶ф敮鎸佺殑鍒楁暟涓256鍒
HSSFCell cell = row.createCell((short) 0);
//璁剧疆姝ゅ崟鍏冩牸镄勬牸寮忎负鏂囨湰锛屾ゅ彞鍙浠ョ渷鐣ワ纴Excel浼氲嚜锷ㄨ瘑鍒銆
//鍏朵粬杩樻湁鍑犵嶅父鐢ㄧ殑镙煎纺锛岃峰弬钥冩湰鏂囧簳閮ㄧ殑琛ュ厖閮ㄥ垎銆
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
//姝ゅ勬槸3.0.1鐗堢殑鏀硅繘涔嫔勶纴涓娄竴鐗埚彲浠ョ洿鎺setCellValue("Hello, World!")锛
//浣嗘槸鍦3.0.1閲岋纴琚玠eprecated浜嗐
cell.setCellValue(new HSSFRichTextString("Hello, World!"));
//鍒涘缓涓涓鏂囦欢杈揿嚭娴侊纴鎸囧畾鍒癈鐩樻牴鐩褰曚笅锛圕鐩橀兘链夊惂锛燂级
//xls鏄疎xcel97-2003镄勬爣鍑嗘墿灞曞悕锛2007鏄痻lsx锛岀洰鍓岖殑POI鑳界洿鎺ョ敓浜х殑杩樻槸xls镙煎纺锛
//濡傛灉姝ゅ勬妸镓╁𪾢钖嶆敼鎴恱lsx锛屽湪鐢‥xcel2007镓揿紑姝ゆ枃浠舵椂浼氭姤阌欍
FileOutputStream writeFile = new FileOutputStream("c:/helloworld.xls");
//鎶奥orkBook鍐椤埌娴侀噷
wb.write(writeFile);
//璁板缑镓嫔姩鍏抽棴娴侊纴瀹樻柟鏂囨。宸茬粡锅氢简鐗瑰埆璇存槑锛岃碢OI涓嶈礋璐e叧闂鐢ㄦ埛镓揿紑镄勬祦銆傛墍浠...
writeFile.close();
涓婇溃灏辨槸鍒涘缓涓涓鏂版枃妗g殑绠鏄扑唬镰侊纴涓嬮溃镄勪緥瀛愭槸璇诲彇鍒氭墠鍒涘缓镄凟xcel骞舵妸璇诲彇鍒扮殑鍐呭规樉绀哄湪鎺у埗鍙颁笂銆
//鎸囧畾瑕佽诲彇镄勬枃浠讹纴链渚嬩娇鐢ㄤ笂闱㈢敓鎴愮殑helloworld.xls
FileInputStream readFile = new FileInputStream("c:/helloworld.xls");
//鍒涘缓涓涓猈orkBook锛屼粠鎸囧畾镄勬枃浠舵祦涓鍒涘缓锛屽嵆涓婇溃鎸囧畾浜嗙殑鏂囦欢娴
HSSFWorkbook wb = new HSSFWorkbook(readFile);
//銮峰彇钖岖О涓衡沧祴璇曢〉钬濈殑sheet
//娉ㄦ剰锛屽傛灉涓嶈兘纭瀹氩叿浣撶殑钖岖О锛屽彲浠ョ敤getSheetAt(int)鏂规硶鍙栧缑Sheet
HSSFSheet st = wb.getSheet("娴嬭瘯椤");
//銮峰缑绗涓琛岋纴钖屼笂锛屽傛灉姝よ屾病链夎鍒涘缓杩囧垯鎶涘嚭寮傚父
HSSFRow row = st.getRow(0);
//銮峰彇绗涓涓鍗曞厓镙硷纴濡傛灉娌℃湁琚鍒涘缓杩囧垯鎶涘嚭寮傚父
HSSFCell cell = row.getCell((short) 0);
//鎶奵ell涓镄勫唴瀹规寜瀛楃︿覆鏂瑰纺璇诲彇鍑烘潵锛屽苟鏄剧ず鍦ㄦ带鍒跺彴涓
//娉ㄦ剰锛実etRichStringCellValue()鏂规硶鏄3.0.1鏂拌拷锷犵殑锛
//钥佺増链涓镄刧etStringCellValue()鏂规硶琚玠eprecated浜
System.out.println(cell.getRichStringCellValue());
//璁板缑鍏抽棴娴
readFile.close();
涓婇溃瀵瑰垱寤哄拰璇诲彇鍒嗗埆涓句緥璇存槑锛屽洖椤句袱娈典唬镰侊纴涓嶉毦鐪嫔嚭POI镎崭綔Excel镄勨滃楄矾钬濓细
•銮峰缑涓涓猈orkBook锛埚嗳纭璇存槸HSSFWorkBook锛屽逛簬POI𨱒ヨ达纴WorkBook鏄钬滃彟链夊叾绫烩濓纴浠ヤ笅绫诲悓锛
•銮峰缑瑕佽/鍐欑殑Sheet瀵硅薄
•銮峰缑瑕佹搷浣灭殑Row瀵硅薄
•銮峰缑链灏忓崟浣嵝斺擟ell瀵硅薄
铹跺悗灏卞彲浠ラ殢镒忕殑璇诲彇銆佸啓鍏ヤ简銆
--------------------------------------------------------------------------------
鍏充簬鍗曞厓镙兼牸寮忕殑琛ュ厖锛
鍗曞厓镙间竴鍏辨湁濡备笅鍑犵嶆牸寮忥纴 閮芥槸HSSFCell绫荤殑闱欐佸叡链夊睘镐э纴
•CELL_TYPE_NUMERIC - 鏁板瓧镙煎纺
•CELL_TYPE_STRING - 瀛楃︿覆锛堥粯璁わ级
•CELL_TYPE_FORMULA - 鍏寮
•CELL_TYPE_BLANK - 绌虹槠
•CELL_TYPE_BOOLEAN - 甯冨皵
•CELL_TYPE_ERROR - 阌栾
绗浜岃 锛氩崟鍏冩牸杈规嗐佸瓧浣揿强棰滆壊
姝ゆ枃姒傝佹х殑璁茶堪浜嗕竴涓嫔崟鍏冩牸镄勮竟妗嗐佸瓧浣扑互鍙婇滆壊镄勮剧疆鏂瑰纺锛屽湪POI涓锛岃繖涓鍒囬兘鏄阃氲繃瀹炰緥鍖朒SSFCellStyle瀵硅薄𨱒ュ疄鐜扮殑锛孒SSFCellStyle绫昏缮链夊緢澶氩叾浠栧疄闄呮湁鐢ㄦ柟娉曪纴链渚嬩腑鍙鏄鍙栧钩镞剁敤镄勬瘆杈冩櫘阆岖殑涓浜涜剧疆𨱒ュ仛婕旂ず镄勚傚ソ锛屽紑濮嬭茶В浜嗏︹
// 璁剧疆琛屽彿
row.setHeightInPoints((short) 50);
// 璁剧疆鍒楀斤纴(256 * 50)杩欑嶅啓娉曟槸锲犱负width鍙傛暟鍗曚綅鏄鍗曚釜瀛楃︾殑256鍒嗕箣涓
st.setColumnWidth(cell.getCellNum(), (short) (256 * 50));
// 璁〩SSFWorkbook鍒涘缓涓涓鍗曞厓镙兼牱寮忕殑瀵硅薄
// 灏忔妧宸э细鍦ㄥ氩勭敤鍒板畬鍏ㄤ竴镙风殑镙峰纺镄勬椂鍊椤彲浠ョ敤宸ュ巶妯″纺鐢熶骇
HSSFCellStyle cellStyle = wb.createCellStyle();
// 璁剧疆鍗曞厓镙肩殑妯钖戝拰绾靛悜瀵归绨鏂瑰纺锛屽叿浣揿弬鏁板氨涓嶅垪浜嗭纴鍙傝仅SSFCellStyle
cellStyle.setAlignment(HSSFCellStyle.ALIGN_JUSTIFY);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 杩欎釜鍦版柟鏄鐢ㄦ潵鍦ㄥ崟鍏冩牸閲岀敾鏂灭嚎镄
// 铡熺悊鏄鍦ㄦ寚瀹氱殑涓や釜镣逛箣闂寸敾绾匡纴铹跺悗榛樿ゆ儏鍐垫ょ嚎浼氶殢镌鍗曞厓镙肩殑鍙桦寲钥屽彉鍖
// 绫讳技Excel闾g嶈剧疆杈规嗙殑鏂瑰纺杈惧埌镄勬枩绾挎晥鏋灭洰鍓嶅ソ镀厮OI涓嶆敮鎸
// 濡傛灉鏄鎴戠枏蹇戒简璇疯板缑锻婅瘔鎴戜竴浜涳纴鍏堣岃阿杩囧暒
HSSFPatriarch patriarch = st.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor();
anchor.setAnchor(cell.getCellNum(), row.getRowNum(), 0, 0, (short) (cell.getCellNum() + 1),
row.getRowNum() + 1, 0, 0);
patriarch.createSimpleShape(anchor);
// 璁剧疆鍗曞厓镙肩殑鏂囨湰鏂瑰纺涓哄彲澶氲岀紪鍐欐柟寮
cellStyle.setWrapText(true);
// 璁剧疆鍗曞厓镙肩殑濉鍏呮柟寮忥纴浠ュ强鍓嶆櫙棰滆壊鍜岃儗鏅棰滆壊
// 涓夌偣娉ㄦ剰锛
// 1.濡傛灉闇瑕佸墠鏅棰滆壊鎴栬儗鏅棰滆壊锛屼竴瀹氲佹寚瀹氩~鍏呮柟寮忥纴涓よ呴‘搴忔棤镓璋掳绂
// 2.濡傛灉钖屾椂瀛桦湪鍓嶆櫙棰滆壊鍜岃儗鏅棰滆壊锛屽墠鏅棰滆壊镄勮剧疆瑕佸啓鍦ㄥ墠闱锛
// 3.鍓嶆櫙棰滆壊涓嶆槸瀛椾綋棰滆壊銆
cellStyle.setFillPattern(HSSFCellStyle.DIAMONDS);
cellStyle.setFillForegroundColor(HSSFColor.RED.index);
cellStyle.setFillBackgroundColor(HSSFColor.LIGHT_YELLOW.index);
// 璁剧疆鍗曞厓镙煎簳閮ㄧ殑杈规嗗强鍏舵牱寮忓拰棰滆壊
// 杩欓噷浠呰剧疆浜嗗簳杈硅竟妗嗭纴宸﹁竟妗嗐佸彸杈规嗗拰椤惰竟妗嗗悓鐞嗗彲璁
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_SLANTED_DASH_DOT);
cellStyle.setBottomBorderColor(HSSFColor.DARK_RED.index);
// 鍒涘缓涓涓瀛椾綋瀵硅薄锛屽洜涓哄瓧浣扑篃鏄鍗曞厓镙兼牸寮忕殑涓閮ㄥ垎锛屾墍浠ヤ粠灞炰簬HSSFCellStyle
// 涓嬮溃鍑犱釜瀛椾綋镄勭浉鍏宠剧疆链涙枃鐢熶箟锛屽氨涓岖敤涓涓璇存槑浜嗗惂
HSSFFont font = wb.createFont();
font.setFontName("瀹嬩綋");
font.setItalic(true);
font.setColor(HSSFColor.BLUE.index);
font.setFontHeightInPoints((short) 20);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 灏嗗瓧浣揿硅薄璧嫔肩粰鍗曞厓镙兼牱寮忓硅薄
cellStyle.setFont(font);
// 灏嗗崟鍏冩牸镙峰纺瀵瑰簲搴旂敤浜庡崟鍏冩牸
cell.setCellStyle(cellStyle);
杩欐牱灏监K浜嗭纴鍙浠ュ彲浠ョ湅鍒版晥鏋滀简銆
琛ュ厖锛屽悇涓绔犺妭镄勪緥瀛愭渶缁堥兘鍦ㄥ悓涓涓闄勪欢鍐咃纴涓嬭浇镄勬椂鍊欎笅浠讳綍涓涓椤甸溃鍐呯殑閮藉彲浠ワ纴閮芥槸涓镙风殑銆
Java-Excel鎶ヨ〃寮鍙慞OI钬斿悎骞躲佸垎缁勫强鍐荤粨
Java-Excel鎶ヨ〃寮鍙慞OI绯诲垪璁插骇
绗涓夎 锛氩崟鍏冩牸镄勫悎骞躲佹暟鎹琛岀殑鍒嗙粍浠ュ强Excel绐楀彛镄勫喕缁
链𨱒ユ兂鎶婅繖涓変釜涓滆タ鍒嗗紑𨱒ュ疄鐜扮殑锛屽悗𨱒ュ彂鐜板垎寮钖庡唴瀹归兘寰埚皯锛屼簬鏄灏卞悎鍦ㄤ竴璧疯村惂銆傞偅镐讳笉鑳藉共宸村反镄勫垪鍑犱釜渚嫔瓙灏卞畬浜嗗惂锛岄偅灏辨嬁姣旇缉鍒濈骇浣嗗张浼氱粡甯搁亣鍒扮殑琛ㄦ牸绫绘暟鎹镄勭粺璁$殑璁捐℃潵锅氢釜灏忎緥瀛愩(婧愮爜涓嬭浇)
缁撴灉鍙戠幇钬斺旇缮鐪熷熻緵鑻︾殑銆
杩欐″厛鐪嬫晥鏋滃浘钖э纴鍏朵腑镄勭珫鎺掑苟涓嶆槸鐪熸f剰涔変笂Excel闾g嶈剧疆镄勭珫鎺掞纴钥屾槸绋嶅井杞鍙树简涓涓嬭緭鍑虹殑鏂瑰纺瀹炵幇镄勶纴锲犱负钥佸栫殑鑻辨枃鍗曡瘝娌℃湁杩欑岖珫鎺掔殑鍙鑳斤纸椤跺氭槸镞嬭浆锛屼絾鏄闾f牱瀛椾綋灏卞彉浜嗭级銆傞櫎姝や箣澶栨兂鍒扮殑鍙﹀栦竴绉岖珫鎺掓枃瀛楃殑瀹炵幇鏂瑰纺灏辨槸镙峰纺镞嬭浆+瀛椾綋镞嬭浆锛屾病娴嬭瘯锛屼笉鐭ラ亾鏄钖﹀彲鐢锛岃皝链夊姛澶瀹炵幇涓涓嬶纴铹跺悗璁板缑锻婅瘔鎴戠粨鏋滃晩銆
钥佹牱瀛愶纴鎶婃牳蹇幂殑浠g爜鍜岀亩瑕佺殑璇存槑鍒楀嚭𨱒ュぇ瀹剁湅涓涓嫔惂銆
// 杩欓噷棣栧厛鍒涘缓涓涓鍗曞厓镙兼牱寮忓硅薄锛岃剧疆浜嗗洓锻ㄧ殑杈规嗕互鍙婂瓧浣揿彲浠ユ崲琛
// 鍏朵腑镄勫瓧浣撴崲琛屾槸鐢ㄦ潵绔栧悜鏄剧ず鍏朵腑镄勪竴涓鍗曞厓镙肩殑
// 镟村ソ镄勪竴镣瑰効锅氭硶鏄鍐嶅仛涓涓鍗旷嫭镄勫崟鍏冩牸镙峰纺瀵硅薄
// 瑕佷笉铹跺湪澶勭悊镊锷ㄥ垪瀹界殑镞跺椤彲鑳戒细链夌偣鍎垮皬闂棰
HSSFCellStyle normalStyle = wb.createCellStyle();
normalStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
normalStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
normalStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
normalStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
normalStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
normalStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
normalStyle.setWrapText(true);
// 钖埚苟鍗曞厓镙糀1-C1锛屽~鍏ュ唴瀹圭殑镞跺欐坊鍒扮涓涓镙煎瓙閲屽氨鍙浠
// 浣嗘槸娉ㄦ剰涓镣癸细鍗曞厓镙煎悎骞跺缌璁剧疆杈规嗗彧鍦ㄥ师绗涓涓涓婃湁鏁堬纴
// 濡傛灉𨱍冲簲鐢ㄧ殑钖埚苟寰岀殑鏁翠綋锛屽垯闇瑕佷竴涓涓镄凛reate鍑哄崟鍏冩牸骞跺簲鐢ㄦ牱寮
// 杩欎釜鏄庢樉鏄涓涓涓嶅お鏂逛究镄勬搷浣滐纴链熷緟POI涓嬩竴鐗堢殑鏀硅繘浜
st.addMergedRegion(new Region(0, (short) 0, 0, (short) 2));
HSSFRow row = st.createRow(0);
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue(new HSSFRichTextString("涓氩姟涓瑙堣〃"));
cell.setCellStyle(normalStyle);
row.createCell((short) 1).setCellStyle(normalStyle);
row.createCell((short) 2).setCellStyle(normalStyle);
// 璁剧疆鍒楀ご锛屽綋铹朵篃鍙浠ヤ竴涓涓涓镙煎瓙镄勫啓锛岀敤寰鐜镒熻夋湁浜涘彇宸ц屽凡
// 钖屾牱锛岄渶瑕佸崟镫缁欐疮涓鍗曞厓镙煎簲鐢ㄦ牱寮忓硅薄
String[] seasonName = ;
for (short i = 3; i < 7; i++)
{
cell = row.createCell(i);
cell.setCellValue(new HSSFRichTextString(seasonName[i - 3]));
cell.setCellStyle(normalStyle);
}
// 杩欎釜鏄绔栨帓鏂囧瓧镄勫疄鐜
// 鐩鍓峆OI娌℃垒鍒帮纸鎴栬告病鎻愪緵锛屾垨璁告垜镞犵煡锛夎╂枃瀛楃珫鎺掔殑鏂规硶锛
// HSSFCellStyle.setRotation()鏂规硶鏄璁剧疆镞嬭浆瑙掑害镄勶纴鍜岀珫鎺掍笉澶涓镙凤纴
// 钖庢潵𨱍充简涓涓嬶纴锲犱负鍙链変腑鏂囩瓑鍏ㄨ掑瓧绗︽墠链夌珫鎺掔殑鍙鑳斤纴
// 涓涓鑻辨枃鍗曡瘝瑕佹槸绔栨帓鐪嬭捣𨱒ユ亹镐曚细闱炲父镐寮傦纴涓嶈繃涓嶆帓闄ゆ闷镩烘湳镄勨︹
st.addMergedRegion(new Region(1, (short) 0, 6, (short) 0));
row = st.createRow(1);
cell = row.createCell((short) 0);
cell.setCellValue(new HSSFRichTextString("鍦\n鍖\n浠\n鐞\nA"));
cell.setCellStyle(normalStyle);
for (int i = 2; i < 7; i++)
st.createRow(i).createCell((short) 0).setCellStyle(normalStyle);
// 灞炰簬鍦板尯镄勪簩绾у垎绫伙纴绔栧悜钖埚苟鐩搁偦镄勪袱涓鍗曞厓镙硷纴鍏朵粬钖屼笂
String[] shopName = ;
for (int i = 1; i < 6; i = i + 2)
{
row = st.createRow(i);
cell = row.createCell((short) 1);
cell.setCellValue(new HSSFRichTextString(shopName[(i - 1) / 2]));
cell.setCellStyle(normalStyle);
st.createRow(i + 1).createCell((short) 1).setCellStyle(normalStyle);
st.addMergedRegion(new Region(i, (short) 1, i + 1, (short) 1));
}
// 灞炰簬杩为挛搴楃殑涓嬩竴绾э纴锘烘湰涔熸槸鍒涘缓鍑烘潵铹跺悗璧嫔+搴旂敤镙峰纺
for (int i = 1; i < 7; i = i + 2)
{
cell = st.getRow(i).createCell((short) 2);
cell.setCellValue(new HSSFRichTextString("鏀跺叆"));
cell.setCellStyle(normalStyle);
cell = st.getRow(i + 1).createCell((short) 2);
cell.setCellValue(new HSSFRichTextString("鏀鍑"));
cell.setCellStyle(normalStyle);
}
// 鏁版嵁閮ㄥ垎锛岀洿鎺Create铹跺悗搴旂敤镙峰纺锛屾湁鏁版嵁镄勮瘽杩欎釜鍦版柟灏辨墦鏁版嵁濂戒简
for (int i = 1; i < 7; i++)
for (short j = 3; j < 7; j++)
st.createRow(i).createCell(j).setCellStyle(normalStyle);
// 鍐荤粨Excel镄勭獥鍙o纴杈圭晫涓烘暟鎹閮ㄥ垎镄勮竟鐣
st.createFreezePane(3, 1);
// 鎸夌収杩为挛搴楃骇鍒鍒嗙粍锛埚綋铹跺疄闄呮儏鍐佃繖镙峰垎缁勬病鍟ユ剰涔夛级
for (int i = 1; i < 7; i = i + 2)
st.groupRow(i, i);
// 鎸夌収鍦板尯鍒嗙粍
st.groupRow(1, 5);
鍏跺疄杩欐牱瀹炵幇璧锋潵鏄涓嶆槸寰堥夯鐑﹀憿锛熺瓟妗堟槸锛氭槸銆
鍏跺疄杩椤彧鏄涓句釜渚嫔瓙锛岀啛鎭変竴涓婸OI镄勫悇绉岮PI钥屽凡锛岀湡姝h佸疄鐜拌繖镙蜂竴涓琛ㄦ牸镄勬椂鍊欙纴渚嫔傞”鐩闇瑕佸埗浣沧姤琛ㄧ瓑绛夛纴阃氩父镄勫仛娉曢兘鏄浜嫔厛鎶婃牸寮忎竴鍒囩殑涓滆タ閮芥坠锷ㄥ埗浣滃ソ锛堣繖涓锅氩ソ浜嗙殑鏂囦欢鍦ㄥ疄闄呯殑椤圭洰閲屾垜浠绉颁箣涓衡沧暟鎹妯℃澘钬濓纴绠绉扳沧ā𨱒库濓级锛岀劧钖庡湪Java搴旂敤涓阃傚綋镄勬椂链烘妸杩欎釜鏂囦欢璇昏繘𨱒ヤ慨鏀癸纴链钖庡啀鍙﹀瓨鍒版寚瀹氱殑浣岖疆鎴栬呬紶阃掔粰涓嬩竴涓澶勭悊钥咃纸渚嫔备互娴佺殑鏂瑰纺阃佺粰Servlet绛夌瓑锛夛纴杩欐牱鍏跺疄POI鍏蜂綋锅氱殑浜嬫儏灏辨槸钖戞ā𨱒块噷鍐欎笟锷$殑鏁版嵁锛岃缮鏄寰堟柟渚垮揩鎹风殑銆