怎么用Java读取word文档里的内容格式信息,比如标题字体,颜色和段间距什么的

怎么用Java读取word文档里的内容格式信息,比如标题字体,颜色和段间距什么的,第1张

Java 有相应的API库能够处理这个,代码我就不给你具体写了,API库的名字是 Apache POI,是专门处理Microsoft Office相关文件的,给个参考链接吧:

>

使用java中的io进行读取

BufferedReader bufferedReader = null;

File file = new File("文档地址+文档名docx");

if(!fileexists()){

Systemoutprintln("文件不存在");

} else {

bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "读取的字符格式(UTF-8或GBK)"));

String lineText = null;

while((lineText = bufferedReaderreadLine()) != null){

if (linText != null && !lineTexteq("")){

Systemoutprintln("一次读取一行,一行内容为:" + lineText);

}

}

}

纯手写的,这里面有点不好写,记得加try,catch还有在finally中释放资源

c#的代码如下:

#region 导入excel数据

private void button2_Click(object sender, EventArgs e)

{

OpenFileDialog openFileDialog = new OpenFileDialog();

openFileDialogFilter = "表格文件 (xls)|xls";

openFileDialogRestoreDirectory = true;

openFileDialogFilterIndex = 1;

if (openFileDialogShowDialog() == DialogResultOK)

{

Import(openFileDialogFileName);

}

}

/// <summary>

/// 导入excel数据

/// </summary>

/// <param name="filePath"></param>

/// <returns></returns>

public static bool Import(string filePath)

{

try

{

//Excel就好比一个数据源一般使用

//这里可以根据判断excel文件是03的还是07的,然后写相应的连接字符串

string strConn = "Provider=MicrosoftJetOLEDB40;" + "Data Source=" + filePath + ";" + "Extended Properties=Excel 80;";

OleDbConnection con = new OleDbConnection(strConn);

conOpen();

string[] names = GetExcelSheetNames(con);

if (namesLength > 0)

{

foreach (string name in names)

{

OleDbCommand cmd = conCreateCommand();

cmdCommandText = stringFormat(" select from [{0}]", name);//[sheetName]要如此格式

OleDbDataReader odr = cmdExecuteReader();

while (odrRead())

{

if (odr[0]ToString() == "序号")//过滤列头 按你的实际Excel文件

continue;

//数据库添加 *** 作

/进行非法值的判断

添加数据到数据表中

添加数据时引用事物机制,避免部分数据提交

Add(odr[1]ToString(), odr[2]ToString(), odr[3]ToString());//数据库添加 *** 作,Add方法自己写的

/

}

odrClose();

}

}

return true;

}

catch (Exception)

{

return false;

}

}

/// <summary>

/// 查询表名

/// </summary>

/// <param name="con"></param>

/// <returns></returns>

public static string[] GetExcelSheetNames(OleDbConnection con)

{

try

{

SystemDataDataTable dt = conGetOleDbSchemaTable(OleDbSchemaGuidTables, new[] { null, null, null, "Table" });//检索Excel的架构信息

var sheet = new string[dtRowsCount];

for (int i = 0, j = dtRowsCount; i < j; i++)

{

//获取的SheetName是带了$的

sheet[i] = dtRows[i]["TABLE_NAME"]ToString();

}

return sheet;

}

catch

{

return null;

}

}

//下面这种方法获取excel Worksheets Name时,提示无法访问该exceL文件,所以改为上面获取工作簿名的方式

///// <summary>

///// 获得excel sheet所有工作簿名字

///// </summary>

///// <param name="filePath"></param>

///// <returns></returns>

//public static string[] GetExcelSheetNames(string filePath)

//{

// MicrosoftOfficeInteropExcelApplicationClass excelApp = new MicrosoftOfficeInteropExcelApplicationClass();

// MicrosoftOfficeInteropExcelWorkbooks wbs = excelAppWorkbooks;

// MicrosoftOfficeInteropExcelWorkbook wb = wbsOpen(filePath, TypeMissing, TypeMissing, TypeMissing, TypeMissing,

// TypeMissing, TypeMissing, TypeMissing, TypeMissing, TypeMissing, TypeMissing,

// TypeMissing, TypeMissing, TypeMissing, TypeMissing);

// int count = wbWorksheetsCount;

// string[] names = new string[count];

// for (int i = 1; i <= count; i++)

// {

// names[i - 1] = ((MicrosoftOfficeInteropExcelWorksheet)wbWorksheets[i])Name;

// }

// return names;

//}

#endregion

以上就是关于怎么用Java读取word文档里的内容格式信息,比如标题字体,颜色和段间距什么的全部的内容,包括:怎么用Java读取word文档里的内容格式信息,比如标题字体,颜色和段间距什么的、如何利用Java-JACOB *** 作WORD文档、java 读取 word文档的内容 并存到数据库 (批量上传试题)主要是怎么取到数据等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/9497672.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-29
下一篇 2023-04-29

发表评论

登录后才能评论

评论列表(0条)

保存