




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、import java.text.SimpleDateFormat;import java.util.Date;import mons.lang3.StringUtils;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFDataFormat;import org.apache.poi.hssf.usermodel.HSSFDateUtil;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi
2、.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.util.CellRangeAddress;excel行自適應(yīng)高度author lwversion 2019 年8月 27 日 下午 1:59:27*/public class AutoRowHeightUtil自適應(yīng)excel行高author lwdate 2019 年8月 27 日 上午 10:27:33para
3、mcell歹!Jparammargin字體邊距public static void autoRowHeight(HSSFCell cell/*/shortmargin ) if (cell = null ) return ;String cellVal = getStringCellValue (cell ); if (StringUtils. isBlank (cellVal )return)HSSFRow row = cell .getRow();HSSFWorkbook workbook =row .getSheet().getWorkbook();short charHeight =c
4、ell .getCellStyle().getFont( workbook ).getFontHeightInPoi nts();float charPx = charHeight / (float )72 * (float )96; / 字體像素float cellWidthPx = getCellWidth (cell ); / 寬度像素 int charLength = cellVal .length(); /字符長(zhǎng)度int charInCell = ( int )( cellWidthPx / charPx ); / 每 個(gè)列(包括合并的列)的字?jǐn)?shù)/在指定寬度的列中的字符展示行數(shù)int
5、 rowNum= charLength / charInCell + (charLength % charInCell 0 ? 1 : 0);/自適應(yīng)之后的行高點(diǎn)數(shù)short cellHeightPx = ( short ) ( short ) rowNum) * (charHeight + margin );row .setHeightInPoints( cellHeightPx );)/*自適應(yīng)excel行高author lwdate 2019 年8月 27 日 上午 10:27:33*/paramcell歹!Jprivate static String getStringCellValu
6、e(Cellcell )(switch ( cell .getCellType()(case HSSFCell. CELL_TYPE_FORMULAif (cell .getCellStyle().getDataFormat()HSSFDataFormat. getBuiltinFormat (yyyy-MM-dd) (SimpleDateFormatSimpleDateFormat( yyyy-MM-dd);sdf = newreturnsdf .format(HSSFDateUtil. getJavaDate (cell .getNumericCellV alue();returncell
7、 .getCellFormula();case HSSFCell. CELL_TYPE_STRING returncell .getStringCellValue();case HSSFCell. CELL_TYPE_NUMERI:Cif(HSSFDateUtil. isCellDateFormatted ( cell ) StringdateStr =;int style = cell .getCellStyle().getDataFormat();Datedate = cell .getDateCellValue();/對(duì)不同格式的日期類型做不同的輸出,與單 元格格式保持一致switch
8、( style ) case 178: dateStr = new SimpleDateFormat( yyyy 年M月d日).format( date ); break ;case 14:dateStr = new SimpleDateFormat( yyyy-MM-dd ).format( date );break ; case 179:dateStr = newSimpleDateFormat( yyyy/MM/dd HH:mm ).format( date ); break ;case 181: dateStr = newSimpleDateFormat(yyyy/MM/dd HH:m
9、m a ).format( date );break ;case 22:dateStr = new SimpleDateFormat( yyyy/MM/dd HH:mm:ss ).format( date );break ;default : break ;return dateStr ;elsecell .setCellType(Cell. CELL_TYPE_STRING return cell .getStringCellValue();case HSSFCell. CELL_TYPE_BOOLEANreturn cell .getBooleanCellValue() ? TRUE: F
10、ALSE; default :return ;/*/獲取單元格及合并單元格的寬度author lwdate 2019 年6月 28 日 下午 1:58:42paramcell 單元格private staticfloat getCellWidth(HSSFCellcell )return單元格及合并單元格的寬度(像素))=cell .getSheet();sheetif (cell = null return 0; HSSFSheetint rowindex = cell .getRowIndex();int columnindex = cell .getColumnindex(); floa
11、t width =sheet .getColumnWidthinPixels( columnindex );boolean isPartOfRegion = false ;intfirstColumn= 0;intlastColumn = 0;int firstRow = 0;int lastRow = 0;int sheetMergeCount = sheet .getNumMergedRegions();for ( int i = 0; i sheetMergeCount ; i +)(CellRangeAddressca =sheet .getMergedRegion( i );firstColumn = ca.getFirstColumn();lastColumn = ca.getLastColumn();firstRow = ca.getFirstRow();lastRow = ca.getLastRow();if (rowindex = firstRow & rowindex = lastRow ) (if ( columnindex = firstColumn & columnindex = la
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 湘教版巨人教學(xué)課件
- 中南民族大學(xué)《陳設(shè)與展示設(shè)計(jì)》2023-2024學(xué)年第二學(xué)期期末試卷
- 煙臺(tái)文化旅游職業(yè)學(xué)院《藝術(shù)欣賞》2023-2024學(xué)年第二學(xué)期期末試卷
- 鐵嶺師范高等專科學(xué)?!稊?shù)據(jù)庫應(yīng)用技術(shù)》2023-2024學(xué)年第二學(xué)期期末試卷
- 鹽城工業(yè)職業(yè)技術(shù)學(xué)院《幼兒教師禮儀》2023-2024學(xué)年第二學(xué)期期末試卷
- 上?,F(xiàn)代化工職業(yè)學(xué)院《景觀設(shè)計(jì)綜合》2023-2024學(xué)年第二學(xué)期期末試卷
- 黑龍江工商學(xué)院《勞動(dòng)教育II》2023-2024學(xué)年第二學(xué)期期末試卷
- 新鄉(xiāng)醫(yī)學(xué)院《政治經(jīng)濟(jì)學(xué)2》2023-2024學(xué)年第二學(xué)期期末試卷
- 集美大學(xué)《可再生能源及其發(fā)電技術(shù)》2023-2024學(xué)年第二學(xué)期期末試卷
- 沈陽藥科大學(xué)《理財(cái)與稅收籌劃》2023-2024學(xué)年第二學(xué)期期末試卷
- 外墻腳手架懸挑專項(xiàng)施工方案
- 2025年中級(jí)會(huì)計(jì)實(shí)務(wù)考試解析方法試題及答案回顧
- 2025至2030年中國(guó)雞胸行業(yè)投資前景及策略咨詢報(bào)告
- 煤礦重大危險(xiǎn)源評(píng)估
- 食品許可證初級(jí)考試試題及答案
- 生物+昆一中2025屆高三聯(lián)考試卷及答案(九)
- 神奇校車 測(cè)試題及答案
- 家政考試題及答案大全
- 成品檢驗(yàn)培訓(xùn)課件
- 《政策性金融工具》課件
- 重度貧血護(hù)理疑難病例討論
評(píng)論
0/150
提交評(píng)論