怎样编写一个最简单的java程序
你是用工具呢,还是不用呢。
如果是工具,比如myeclipse,那么就是创建一个类,然后敲入main,alt+/可以导出main函数(Java程序的入口就是main)。
如果没有用工具,需要配置环境变量的,配置好之后。
建一个Java文件,文件名字和类名相同,包括大小写,然后public class Demo{public static void main(String args[]){System.out.println("Hello World");}}这个system首个字母是大写,切记。
然后再控制台,找到路径,javac Demo.java,产生.class文件,然后再java Demo执行,输出Hello World。
java poi 操作word文档,怎么写入带上下标的文字?
步骤第一步,使用输入流打开文件,并获得文档的XWPFDocument对象。
然后获得文档的所有段落,进而获得要操作的文本框所在的段落,具体使用时候,可以通过判断或者print操作得知要操作的文本框到底是哪一段。
FileInputStream fis = newFileInputStream("e:/file.docx");XWPFDocument doc = new XWPFDocument(fis);List paragraphList =doc.getParagraphs();XWPFParagraph paragraph = paragraphList.get(10);文本框在Word中显示第二步,获取XWPFParagraph的XmlObject,然后获得XmlObject对象的游标。
可以通过打印XmlObject来得知当前XML的内容,也可以使用XmlCursor的getName方法和getTextValue方法来查看当前游标所在位置的Node及Node的值。
XmlObject object =paragraph.getCTP().getRArray(1);XmlCursor cursor = object.newCursor();第四步,通过移动游标,找到要修改的文本所在位置,然后使用游标的setTextValue来设置其值。
//修改第一处文本:cursor.toChild(1); cursor.toChild(0);cursor.toChild(3); cursor.toChild(0); cursor.toChild(0); cursor.toChild(3);cursor.toChild(1); cursor.setTextValue("First");// 修改第二处文本cursor.toParent(); cursor.toParent();cursor.toChild(1);cursor.toChild(3); cursor.toChild(1);cursor.setTextValue("Second");第四步,保存文件、关闭输入输出流。
FileOutputStream fos = newFileOutputStream("e:/export.docx");doc.write(fos);fos.flush();fos.close();fis.close();修改后的文本框
eclipse怎么编写JAVA,然后运行
应该是编写一个Java小的Java项目吧?1.新建一个Java project 取个名字。
其他的不管。
2.然后再文件上右键创建一个class文件,名字开头要大写。
其他的不管。
3.然后在打开的你建立的 (你取的文件名 ).java 中编写你的程序。
4.编写完之后右键点击,run as 选择 Java application5.在控制台上就会显示了。
简单的一个hello word 程序Javaproject就叫demo吧, 新建class的名字Text.javaText文件中的代码:********************************************************* public class Text{public static void main(String [] args){System.out.print("你好,834487!");}} *********************************************************没有星号!! 运行结果就是在下面的某个框中,显示 你好,834487!希望能帮到你!
用java将word文件上传到服务器,把word里面的内容保存到数据库
使用java中的io进行读取BufferedReader bufferedReader = null;File file = new File("文档地址+文档名.docx");if(!file.exists()){System.out.println("文件不存在");} else {bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "读取的字符格式(UTF-8或GBK)"));String lineText = null;while((lineText = bufferedReader.readLine()) != null){if (linText != null && !lineText.eq("")){System.out.println("一次读取一行,一行内容为:" + lineText);}}}
用java将word文件上传到服务器,把word里面的内容保存到数据库
展开全部 使用java中的io进行读取BufferedReader bufferedReader = null;File file = new File("文档地址+文档名.docx");if(!file.exists()){System.out.println("文件不存在");} else {bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "读取的字符格式(UTF-8或GBK)"));String lineText = null;while((lineText = bufferedReader.readLine()) != null){if (linText != null && !lineText.eq("")){System.out.println("一次读取一行,一行内容为:" + lineText);}}}
java题目:编写一个程序使用java的输入,输出流技术将一个文本文件...
import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;/*** * @author Felly* @date 2008年12月29日16:16:12**/public class InOut {//调试的时候文件和class文件在同目录,不然自己加绝对路径public final static String inFile="in.txt";//被写文件public final static String outFile="out.txt";//被读文件public void readAndWrite(String in,String out) throws FileNotFoundException,IOException {File inFile=new File(in);File outFile=new File(out);BufferedReader reader=new BufferedReader(new FileReader(outFile));PrintWriter pw=new PrintWriter(new FileWriter(inFile));String line="";int index=0;while((line=reader.readLine())!=null)//循环到没有内容为止{ index++;line=index+line;//加上行号pw.write(line);//写入一行}//close pw.close();reader.close();}}
怎用用java导入、导入word形式的考试题目?
使用java中的io进行读取BufferedReader bufferedReader = null;File file = new File("文档地址+文档名.docx");if(!file.exists()){System.out.println("文件不存在");} else {bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "读取的字符格式(UTF-8或GBK)"));String lineText = null;while((lineText = bufferedReader.readLine()) != null){if (linText != null && !lineText.eq("")){System.out.println("一次读取一行,一行内容为:" + lineText);
java导出word怎么做,最好有代码
public class CreateWordDemo{ public void createDocContext(String file)throws DocumentException,IOException { //设置纸张大小 Document document = newDocument(PageSize.A4); //建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中 RtfWriter2.getInstance(document, newFileOutputStream(file)); document.open(); //设置中文字体 BaseFont bfChinese =BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED); //标题字体风格 Font titleFont = new Font(bfChinese, 12,Font.BOLD); //正文字体风格 Font contextFont = new Font(bfChinese, 10,Font.NORMAL); Paragraph title = newParagraph("标题"); //设置标题格式对齐方式 title.setAlignment(Element.ALIGN_CENTER); title.setFont(titleFont); document.add(title); String contextString ="iText是一个能够快速产生PDF文件的java类库。
" + " \n"//换行 +"iText的java类对于那些要产生包含文本," + "表格,图形的只读文档是很有用的。
它的类库尤其与javaServlet有很好的给合。
" +"使用iText与PDF能够使你正确的控制Servlet的输出。
"; Paragraph context = newParagraph(contextString); //正文格式左对齐 context.setAlignment(Element.ALIGN_LEFT); context.setFont(contextFont); //离上一段落(标题)空的行数 context.setSpacingBefore(5); //设置第一行空的列数 context.setFirstLineIndent(20); document.add(context); //利用类FontFactory结合Font和Color可以设置各种各样字体样式 Paragraph underline = new Paragraph("下划线的实现",FontFactory.getFont( FontFactory.HELVETICA_BOLDOBLIQUE, 18,Font.UNDERLINE, new Color(0, 0,255))); document.add(underline); // 设置 Table表格 Table aTable = newTable(3); int width[] = { 25, 25, 50}; aTable.setWidths(width);//设置每列所占比例 aTable.setWidth(90); // 占页面宽度90% aTable.setAlignment(Element.ALIGN_CENTER);//居中显示 aTable.setAlignment(Element.ALIGN_MIDDLE);//纵向居中显示 aTable.setAutoFillEmptyCells(true); //自动填满 aTable.setBorderWidth(1); //边框宽度 aTable.setBorderColor(new Color(0, 125, 255)); //边框颜色 aTable.setPadding(2);//衬距,看效果就知道什么意思了 aTable.setSpacing(3);//即单元格之间的间距 aTable.setBorder(2);//边框 //设置表头 Cell haderCell = newCell("表格表头"); haderCell.setHeader(true); haderCell.setColspan(3); aTable.addCell(haderCell); aTable.endHeaders(); Font fontChinese = new Font(bfChinese, 12, Font.NORMAL,Color.GREEN); Cell cell = new Cell(new Phrase("这是一个测试的 3*3 Table 数据",fontChinese)); cell.setVerticalAlignment(Element.ALIGN_TOP); cell.setBorderColor(new Color(255, 0,0)); cell.setRowspan(2); aTable.addCell(cell); aTable.addCell(newCell("#1")); aTable.addCell(newCell("#2")); aTable.addCell(newCell("#3")); aTable.addCell(newCell("#4")); Cell cell3 = new Cell(new Phrase("一行三列数据",fontChinese)); cell3.setColspan(3); cell3.setVerticalAlignment(Element.ALIGN_CENTER); aTable.addCell(cell3); document.add(aTable); document.add(newParagraph("\n")); //添加图片 Image.getInstance即可以放路径又可以放二进制字节流 Image img =Image.getInstance("d:\\img01800.jpg"); img.setAbsolutePosition(0,0); img.setAlignment(Image.RIGHT);//设置图片显示位置 img.scaleAbsolute(60, 60);//直接设定显示尺寸 //img.scalePercent(50);//表示显示的大小为原尺寸的50% // img.scalePercent(25,12);//图像高宽的显示比例 //img.setRotation(30);//图像旋转一定角度 document.add(img); document.close(); } public static void main(String[] args){ CreateWordDemo word = newCreateWordDemo(); String file ="d:/demo1.doc"; try{ word.createDocContext(file); } catch (DocumentException e){ e.printStackTrace(); } catch (IOException e){ e.printStackTrace(); } } }
转载请注明出处51数据库 » java word demo