输出excel文件|java如何输出xls格式的Excel表格文件

输出excel文件|java如何输出xls格式的Excel表格文件的第1张示图

❶ java怎样输出excel文件

//java生成简单的Excel文件packagebeans.excel;importjava.io.IOException;importjava.io.OutputStream;importjxl.Workbook;importjxl.write.Label;importjxl.write.WritableSheet;importjxl.write.WritableWorkbook;importjxl.write.WriteException;publicclassSimpleExcelWrite{publicvoidcreateExcel(OutputStreamos)throwsWriteException,IOException{//创建工作薄WritableWorkbookworkbook=Workbook.createWorkbook(os);//创建新的一页WritableSheetsheet=workbook.createSheet("FirstSheet",0);//创建要显示的内容,创建一个单元格,第一个参数为列坐标,第二个参数为行坐标,第三个参数为内容Labelxuexiao=newLabel(0,0,"学校");sheet.addCell(xuexiao);Labelzhuanye=newLabel(1,0,"专业");sheet.addCell(zhuanye);Labeljingzhengli=newLabel(2,0,"专业竞争力");sheet.addCell(jingzhengli);Labelqinghua=newLabel(0,1,"清华大学");sheet.addCell(qinghua);Labeljisuanji=newLabel(1,1,"计算机专业");sheet.addCell(jisuanji);Labelgao=newLabel(2,1,"高");sheet.addCell(gao);Labelbeida=newLabel(0,2,"北京大学");sheet.addCell(beida);Labelfalv=newLabel(1,2,"法律专业");sheet.addCell(falv);Labelzhong=newLabel(2,2,"中");sheet.addCell(zhong);Labelligong=newLabel(0,3,"北京理工大学");sheet.addCell(ligong);Labelhangkong=newLabel(1,3,"航空专业");sheet.addCell(hangkong);Labeldi=newLabel(2,3,"低");sheet.addCell(di);//把创建的内容写入到输出流中,并关闭输出流workbook.write();workbook.close();os.close();}}

❷ 导出的Excel文件

再导出一次,看看文件的路径。或者以导出的文件名为条件全盘搜索。并不是所有软件默认的导出路径都在“我的文档”里。

❸ 通过打印输出为excel文件

其实所有打印程序都支持的,只是要在打印对话框中勾选“打印到文件”此项就可以了

❹ java如何输出xls格式的Excel表格文件

有个开源的东东-jxl.jar,可以到http://sourceforge.net/project/showfiles.php?group_id=79926下载。一.读取Excel文件内容 /**读取Excel文件的内容 * @param file 待读取的文件 * @return */ public static String readExcel(File file){ StringBuffer sb = new StringBuffer(); Workbook wb = null; try { //构造Workbook(工作薄)对象 wb=Workbook.getWorkbook(file); } catch (BiffException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if(wb==null) return null; //获得了Workbook对象之后,就可以通过它得到Sheet(工作表)对象了 Sheet[] sheet = wb.getSheets(); if(sheet!=null&&sheet.length>0){ //对每个工作表进行循环 for(int i=0;i<sheet.length;i++){ //得到当前工作表的行数 int rowNum = sheet[i].getRows(); for(int j=0;j<rowNum;j++){ //得到当前行的所有单元格 Cell[] cells = sheet[i].getRow(j); if(cells!=null&&cells.length>0){ //对每个单元格进行循环 for(int k=0;k<cells.length;k++){ //读取当前单元格的值 String cellValue = cells[k].getContents(); sb.append(cellValue+" "); } } sb.append(" "); } sb.append(" "); } } //最后关闭资源,释放内存 wb.close(); return sb.toString(); }二.写入Excel文件这里有很多格式了,比如文本内容加粗,加上某些颜色等,可以参考jxl的api,同时还推荐一篇不错的文章:http://www.ibm.com/developerworks/cn/java/l-javaExcel/?ca=j-t10 /**生成一个Excel文件 * @param fileName 要生成的Excel文件名 */ public static void writeExcel(String fileName){ WritableWorkbook wwb = null; try { //首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象 wwb = Workbook.createWorkbook(new File(fileName)); } catch (IOException e) { e.printStackTrace(); } if(wwb!=null){ //创建一个可写入的工作表 //Workbook的createSheet方法有两个参数,第一个是工作表的名称,第二个是工作表在工作薄中的位置 WritableSheet ws = wwb.createSheet("sheet1", 0); //下面开始添加单元格 for(int i=0;i<10;i++){ for(int j=0;j<5;j++){ //这里需要注意的是,在Excel中,第一个参数表示列,第二个表示行 Label labelC = new Label(j, i, "这是第"+(i+1)+"行,第"+(j+1)+"列"); try { //将生成的单元格添加到工作表中 ws.addCell(labelC); } catch (RowsExceededException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } } } try { //从内存中写入文件中 wwb.write(); //关闭资源,释放内存 wwb.close(); } catch (IOException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } } } 三.在一个Excel文件中查找是否包含某一个关键字 /**搜索某一个文件中是否包含某个关键字 * @param file 待搜索的文件 * @param keyword 要搜索的关键字 * @return */ public static boolean searchKeyWord(File file,String keyWord){ boolean res = false; Workbook wb = null; try { //构造Workbook(工作薄)对象 wb=Workbook.getWorkbook(file); } catch (BiffException e) { return res; } catch (IOException e) { return res; } if(wb==null) return res; //获得了Workbook对象之后,就可以通过它得到Sheet(工作表)对象了 Sheet[] sheet = wb.getSheets(); boolean breakSheet = false; if(sheet!=null&&sheet.length>0){ //对每个工作表进行循环 for(int i=0;i<sheet.length;i++){ if(breakSheet) break; //得到当前工作表的行数 int rowNum = sheet[i].getRows(); boolean breakRow = false; for(int j=0;j<rowNum;j++){ if(breakRow) break; //得到当前行的所有单元格 Cell[] cells = sheet[i].getRow(j); if(cells!=null&&cells.length>0){ boolean breakCell = false; //对每个单元格进行循环 for(int k=0;k<cells.length;k++){ if(breakCell) break; //读取当前单元格的值 String cellValue = cells[k].getContents(); if(cellValue==null) continue; if(cellValue.contains(keyWord)){ res = true; breakCell = true; breakRow = true; breakSheet = true; } } } } } } //最后关闭资源,释放内存 wb.close(); return res; } 四.往Excel中插入图片图标插入图片的实现很容易,参看以下代码: /**往Excel中插入图片 * @param dataSheet 待插入的工作表 * @param col 图片从该列开始 * @param row 图片从该行开始 * @param width 图片所占的列数 * @param height 图片所占的行数 * @param imgFile 要插入的图片文件 */ public static void insertImg(WritableSheet dataSheet, int col, int row, int width, int height, File imgFile){ WritableImage img = new WritableImage(col, row, width, height, imgFile); dataSheet.addImage(img); } 以上代码的注释已经很清楚了,大概也就不用再解释了,我们可以用如下程序验证: try { //创建一个工作薄 WritableWorkbook workbook = Workbook.createWorkbook(new File("D:/test1.xls")); //待插入的工作表 WritableSheet imgSheet = workbook.createSheet("Images",0); //要插入的图片文件 File imgFile = new File("D:/1.png"); //图片插入到第二行第一个单元格,长宽各占六个单元格 insertImg(imgSheet,0,1,6,6,imgFile); workbook.write(); workbook.close(); } catch (IOException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); }但是jxl只支持png格式的图片,jpg格式和gif格式都不支持五.插入页眉页脚一般的页眉页脚都分为三个部分,左,中,右三部分,利用如下代码可实现插入页眉页脚 /**向Excel中加入页眉页脚 * @param dataSheet 待加入页眉的工作表 * @param left * @param center * @param right */ public static void setHeader(WritableSheet dataSheet,String left,String center,String right){ HeaderFooter hf = new HeaderFooter(); hf.getLeft().append(left); hf.getCentre().append(center); hf.getRight().append(right); //加入页眉 dataSheet.getSettings().setHeader(hf); //加入页脚 //dataSheet.getSettings().setFooter(hf); }我们可以用如下代码测试该方法: try { //创建一个工作薄 WritableWorkbook workbook = Workbook.createWorkbook(new File("D:/test1.xls")); //待插入的工作表 WritableSheet dataSheet = workbook.createSheet("加入页眉",0); ExcelUtils.setHeader(dataSheet, "chb", "2007-03-06", "第1页,共3页"); workbook.write(); workbook.close(); } catch (IOException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); }六偷懒工具设计之sql2Excel今天在公司陪山东客户调试,远程登录,我在linux下什么工具都没有,用ssh登录服务器,直接用mysql查询数据库,提出记录中的所有汉字全是乱码。哎,可恶的公司,不让我用windows,要不我就可以用putty或者EMS了,我ft! 甚是不爽之下,我决定自己写个工具了,把客户数据库中的数据全部提取并保存到Excel中,这样我不就可以一目了然了嘛,嘿嘿,好吧,那我就写一个工具吧。第一部分就是谁都会的jdbc操作,连接数据库,提取数据集合。 Connection con; Statement state; /**初始化连接 * @param serverIp * @param dataBase * @param userName * @param password * @throws ClassNotFoundException * @throws SQLException */ public void init(String serverIp,String dataBase,String userName,String password) throws ClassNotFoundException, SQLException{ Class.forName("com.mysql.jdbc.Driver"); //配置数据源 String url="jdbc:mysql://"+serverIp+"/"+dataBase+"?useUnicode=true&characterEncoding=GB2312"; con=DriverManager.getConnection(url,userName,password); } /**得到查询结果集 * @param sql * @return * @throws SQLException */ public ResultSet getResultSet(String sql) throws SQLException{ state = con.createStatement(); ResultSet res = state.executeQuery(sql); return res; } /**关闭连接 * @throws SQLException */ public void close() throws SQLException{ if(con!=null) con.close(); if(state!=null) state.close(); }第二部分就是把ResultSet中的记录写入一个Excel文件操作Excel,我用的是jxl,不熟的同学可以参考:利用java操作Excel文件 /**将查询结果写入Excel文件中 * @param rs * @param file * @throws SQLException */ public void writeExcel(ResultSet rs,File file) throws SQLException{ WritableWorkbook wwb = null; try{ //首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象 wwb = Workbook.createWorkbook(file); } catch (IOException e){ e.printStackTrace(); } if(wwb!=null){ WritableSheet ws = wwb.createSheet("sheet1", 0); int i=0; while(rs.next()){ Label label1 = new Label(0, i, rs.getString("id")); Label label2 = new Label(1, i, rs.getString("category")); try { ws.addCell(label1); ws.addCell(label2); } catch (RowsExceededException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } i++; } try { //从内存中写入文件中 wwb.write(); //关闭资源,释放内存 wwb.close(); } catch (IOException e) { e.printStackTrace(); } catch (WriteException e){ e.printStackTrace(); } } }测试程序: Sql2Excel se = new Sql2Excel(); try { se.init("127.0.0.1","mydabase", "root", "1234"); ResultSet rs = se.getResultSet("select id,category from xx "); se.writeExcel(rs, new File("/root/sql2excel.xls")); se.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }

❺ 导出excel的几种方法

方案一: 通过OleDB方式获取Excel文件的数据,然后通过DataSet中转到SQL ServeropenFileDialog = new OpenFileDialog();openFileDialog.Filter = "Excel files(*.xls)|*.xls";if(openFileDialog.ShowDialog()==DialogResult.OK){FileInfo fileInfo = new FileInfo(openFileDialog.FileName);string filePath = fileInfo.FullName;string connExcel = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=Excel 8.0";try{OleDbConnection oleDbConnection = new OleDbConnection(connExcel);oleDbConnection.Open();//获取excel表DataTable dataTable = oleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);//获取sheet名,其中[0][1]…[N]: 按名称排列的表单元素string tableName = dataTable.Rows[0][2].ToString().Trim();tableName = "[" + tableName.Replace("'","") + "]";//利用SQL语句从Excel文件里获取数据//string query = "SELECT classDate,classPlace,classTeacher,classTitle,classID FROM " + tableName;string query = "SELECT 日期,开课城市,讲师,课程名称,持续时间 FROM " + tableName;dataSet = new DataSet();//OleDbCommand oleCommand = new OleDbCommand(query, oleDbConnection);//OleDbDataAdapter oleAdapter = new OleDbDataAdapter(oleCommand);OleDbDataAdapter oleAdapter = new OleDbDataAdapter(query,connExcel);oleAdapter.Fill(dataSet,"gch_Class_Info");//dataGrid1.DataSource = dataSet;//dataGrid1.DataMember = tableName;dataGrid1.SetDataBinding(dataSet,"gch_Class_Info");//从excel文件获得数据后,插入记录到SQL Server的数据表DataTable dataTable1 = new DataTable();SqlDataAdapter sqlDA1 = new SqlDataAdapter(@"SELECT classID, classDate,classPlace, classTeacher, classTitle, rativeDate FROM gch_Class_Info",sqlConnection1);SqlCommandBuilder sqlCB1 = new SqlCommandBuilder(sqlDA1);sqlDA1.Fill(dataTable1);foreach(DataRow dataRow in dataSet.Tables["gch_Class_Info"].Rows){DataRow dataRow1 = dataTable1.NewRow();dataRow1["classDate"] = dataRow["日期"];dataRow1["classPlace"] = dataRow["开课城市"];dataRow1["classTeacher"] = dataRow["讲师"];dataRow1["classTitle"] = dataRow["课程名称"];dataRow1["rativeDate"] = dataRow["持续时间"];dataTable1.Rows.Add(dataRow1);}Console.WriteLine("新插入 " + dataTable1.Rows.Count.ToString() + " 条记录");sqlDA1.Update(dataTable1);oleDbConnection.Close();}catch(Exception ex){Console.WriteLine(ex.ToString());}}//方案二: 直接通过SQL语句执行SQL Server的功能函数将Excel文件转换到SQL Server数据库OpenFileDialog openFileDialog = new OpenFileDialog();openFileDialog.Filter = "Excel files(*.xls)|*.xls";SqlConnection sqlConnection1 = null;if(openFileDialog.ShowDialog()==DialogResult.OK){string filePath = openFileDialog.FileName;sqlConnection1 = new SqlConnection();sqlConnection1.ConnectionString = "server=(local);integrated security=SSPI;initial catalog=Library";//import excel into SQL Server 2000/*string importSQL = "SELECT * into live41 FROM OpenDataSource" + "('Microsoft.Jet.OLEDB.4.0','Data Source=" + "\"" + "E:\\022n.xls" + "\"" + "; User ID=;Password=; Extended properties=Excel 5.0')…[Sheet1$]";*///export SQL Server 2000 into excelstring exportSQL = @"EXEC master..xp_cmdshell'bcp Library.dbo.live41 out " + filePath + "-c -q -S" + "\"" + "\"" +" -U" + "\"" + "\"" + " -P" + "\"" + "\"" + "\'";try{sqlConnection1.Open();//SqlCommand sqlCommand1 = new SqlCommand();//sqlCommand1.Connection = sqlConnection1;//sqlCommand1.CommandText = importSQL;//sqlCommand1.ExecuteNonQuery();//MessageBox.Show("import finish!");SqlCommand sqlCommand2 = new SqlCommand();sqlCommand2.Connection = sqlConnection1;sqlCommand2.CommandText = exportSQL;sqlCommand2.ExecuteNonQuery();MessageBox.Show("export finish!");}catch(Exception ex){MessageBox.Show(ex.ToString());}}if(sqlConnection1!=null){sqlConnection1.Close();sqlConnection1 = null;}//方案三: 通过到入Excel的VBA dll,通过VBA接口获取Excel数据到DataSetOpenFileDialog openFile = new OpenFileDialog();openFile.Filter = "Excel files(*.xls)|*.xls";ExcelIO excelio = new ExcelIO();if(openFile.ShowDialog()==DialogResult.OK){if(excelio!=null)excelio.Close();excelio = new ExcelIO(openFile.FileName);object[,] range = excelio.GetRange();excelio.Close();DataSet ds = new DataSet("xlsRange");int x = range.GetLength(0);int y = range.GetLength(1);DataTable dt = new DataTable("xlsTable");DataRow dr;DataColumn dc;ds.Tables.Add(dt);for(int c=1; c<=y; c++){dc = new DataColumn();dt.Columns.Add(dc);}object[] temp = new object[y];for(int i=1; i<=x; i++){dr = dt.NewRow();for(int j=1; j<=y; j++){temp[j-1] = range[i,j];}dr.ItemArray = temp;ds.Tables[0].Rows.Add(dr);}dataGrid1.SetDataBinding(ds,"xlsTable");if(excelio!=null)excelio.Close();}

❻ 怎么把excel里的文件导出

如果想把EXCEL中的文件导出来的话,可以点击菜单中的文件命令选项,点击下拉菜单,找到导入EXCEL中文件的选项,然后点击打开,这样就可以对EXCEL中的文件进行导出的

❼ 如何快速将数据导出成excel文件格式

类似办法处理吧:本次经验归纳了SQL数据表导出到EXCEL中的三种方法:1、复制粘贴,版将SQL查询语句按指定权条件导出数据;——适合小量数据。2、导出完整的SQL数据表为Excel文件;——适合大量数据。3、定时自动导出指定SQL数据表为Excel文件;——适合大量数据。其它:DTS数据转换服务导入导出向导或者DTS设计器创建DTS包;

❽ 如何在C++中输入程序 执行结果直接输出EXCEl文件

先生成CSV文件,再用Excel导入该CSV文件.这样比较简单.CSV文件就是逗号分隔文件,格式如下:1,2,3,4,5,6a,b,c,d,e,f

❾ excel表格如何导出

怎样导出excel表格1、进入的工作区的一个表格,点击右上角的“导出”。2、选择导出哪些数据:默认为导出全部数据,如果您筛选过数据,则会提供导出筛选后部分数据的选项。3、点击导出后,等候后台将数据导出。导出成功会有弹窗提醒,点击蓝字部分即可下载;如果数据较多,导出时间会较长,您可以暂时关闭弹窗,等待系统通知。4、收到通知后,点击导出成功的通知,即可下载导出的Excel文件。

❿ 关于输出excel文件

这是一个用C语言编的功能模块,将GBK码文件换转成utf-8码文件后输出,

未经允许不得转载:山九号 » 输出excel文件|java如何输出xls格式的Excel表格文件

赞 (0)