import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Font;
public class UnderLineUtils {
private static final String U_START = "<u>";
private static final String U_END = "</u>";
public static void main(String[] args) {
String title = "空气中充满了<u>水份</u>,水是由<u>H</u>元素和<u>O</u>元素组成的。";
List<List<int[]>> tagIndexArr = null;
if (containU(title)) {
tagIndexArr = new ArrayList<List<int[]>>();
title = getUIndexs(title, tagIndexArr);
}
//TODO 文件路径自己改
File f = new File("C:\\tmp\\test.xls");
try {
FileOutputStream fout = new FileOutputStream(f);
// 声明一个工作薄
@SuppressWarnings("resource")
HSSFWorkbook workbook = new HSSFWorkbook();
// 生成一个表格
HSSFSheet sheet = workbook.createSheet("sheet1");
int curRowIndex = 0;
HSSFRow row = sheet.createRow(curRowIndex);
HSSFCell cell = row.createCell(0);
if (tagIndexArr != null) {
HSSFRichTextString text = new HSSFRichTextString(title);
List<int[]> us = tagIndexArr.get(0);
if (us.size() > 0) {
HSSFFont ft = workbook.createFont();
ft.setUnderline(Font.U_SINGLE);
for (int[] pair : us) {
text.applyFont(pair[0], pair[1], ft);
}
}
cell.setCellValue(text);
} else {
cell.setCellValue(title);
}
try {
workbook.write(fout);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 获取下一对标签的index,不存在这些标签就返回null
* @param s
* @param tag U_START
* @return int[]中有两个元素,第一个是开始标签的index,第二个元素是结束标签的index
*/
public static int[] getNextUsTagsIndex(String s, String tag) {
int firstUStart = s.indexOf(tag);
if (firstUStart > -1) {
int firstUEnd = s.indexOf(U_END);
if (firstUEnd > firstUStart) {
return new int[] { firstUStart, firstUEnd };
}
}
return null;
}
/**移除下一对u标签,返回移除后的字符串
* @param s
* @param tag U_START
* @return
*/
public static String removeNextUTags(String s, String tag) {
s = s.replaceFirst(tag, "");
s = s.replaceFirst(U_END, "");
return s;
}
/**
* 判断是不是包含u标签
* @param s
* @return
*/
public static boolean containU(String s) {
return (s.contains(U_START) && s.contains(U_END));
}
/**
* 处理字符串,得到每个u标签的开始和对应的结束的标签的index,方便后面根据这个标签做字体操作
* @param s
* @param tagIndexList 传一个新建的空list进来,方法结束的时候会存储好标签位置信息。
* <br>tagIndexList.get(0)存放的u
*
* @return 返回u处理完之后的字符串
*/
public static String getUIndexs(String s, List<List<int[]>> tagIndexList) {
List<int[]> us = new ArrayList<int[]>();
while (true) {
int[] u_pair = getNextUsTagsIndex(s, U_START);
if (u_pair != null) {
s = removeNextUTags(s, U_START);
//<u>标签被去掉之后,结束标签需要相应往前移动
u_pair[1] = u_pair[1] - U_START.length();
us.add(u_pair);
continue;
}
if (u_pair == null) {
break;
}
}
tagIndexList.add(us);
return s;
}
}