中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

JavaScript Upload & Download

發布時間:2020-08-08 18:05:23 來源:ITPUB博客 閱讀:322 作者:fesir 欄目:編程語言
http://www.blogjava.net/hawk8359/archive/2008/02/19/180623.html[@more@]
JavaScript Upload & Download{
JavaScript Upload & DownloadJavaScript Upload & Download
/**//*
JavaScript Upload & Download * 把數據庫中的字段導入到Excel ,并生成Excel文檔
JavaScript Upload & Download *
*/

JavaScript Upload & Download
public ActionForward getDownload(ActionMapping actionMapping,
JavaScript Upload & Download ActionForm actionForm, HttpServletRequest request,
JavaScript Upload & DownloadJavaScript Upload & Download HttpServletResponse response)
throws Exception JavaScript Upload & Download{
JavaScript Upload & Download Form fm
= (Form) actionForm;
JavaScript Upload & Download
// Excel 文件存放在服務器的相對路徑下
JavaScript Upload & Download
String outputFile = request.getRealPath("/tmp/Excel.xls");
JavaScript Upload & Download
JavaScript Upload & DownloadJavaScript Upload & Download
tryJavaScript Upload & Download{
JavaScript Upload & Download
// 創建新的Excel 工作簿
JavaScript Upload & Download
HSSFWorkbook workbook =new HSSFWorkbook();
JavaScript Upload & Download
// 在Excel 工作簿中建一工作表
JavaScript Upload & Download
HSSFSheet sheet = workbook.createSheet("Sheet1");
JavaScript Upload & Download
// 設置單元格格式(文本)
JavaScript Upload & Download
HSSFCellStyle cellStyle = workbook.createCellStyle();
JavaScript Upload & Download cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat(
"@"));
JavaScript Upload & Download
JavaScript Upload & Download
// 在索引0的位置創建行(第一行)
JavaScript Upload & Download
HSSFRow row = sheet.createRow((short) 0);
JavaScript Upload & Download
JavaScript Upload & Download HSSFCell cell1
= row.createCell((short) 0);// 第一列
JavaScript Upload & Download
HSSFCell cell2 = row.createCell((short) 1);
JavaScript Upload & Download HSSFCell cell3
= row.createCell((short) 2);
JavaScript Upload & Download
// 定義單元格為字符串類型
JavaScript Upload & Download
cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
JavaScript Upload & Download cell2.setCellType(HSSFCell.CELL_TYPE_STRING);
JavaScript Upload & Download cell3.setCellType(HSSFCell.CELL_TYPE_STRING);
JavaScript Upload & Download
JavaScript Upload & Download cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
JavaScript Upload & Download cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
JavaScript Upload & Download cell3.setEncoding(HSSFCell.ENCODING_UTF_16);
JavaScript Upload & Download
// 在單元格中輸入數據
JavaScript Upload & Download
cell1.setCellValue("姓名");
JavaScript Upload & Download cell2.setCellValue(
"性別");
JavaScript Upload & Download cell3.setCellValue(
"年齡");
JavaScript Upload & Download
JavaScript Upload & Download Connection connection
= session.connection();
JavaScript Upload & Download
JavaScript Upload & Download String sql
="Select t.name, t.sex, t.age from table t where t.sex = ?";
JavaScript Upload & Download
JavaScript Upload & DownloadJavaScript Upload & Download
tryJavaScript Upload & Download{
JavaScript Upload & Download PreparedStatement ps
= connection.prepareStatement(sql);
JavaScript Upload & Download ps.setString(
1, fm.getSex());// 傳入查詢條件
JavaScript Upload & Download
ResultSet rs = ps.executeQuery();// 查詢結果存入rs
JavaScript Upload & Download
connection.commit();// 執行SQL
JavaScript Upload & Download

JavaScript Upload & DownloadJavaScript Upload & Download
while (rs.next()) JavaScript Upload & Download{
JavaScript Upload & Download
//設置j行從第二行開始
JavaScript Upload & Download
int j =1;
JavaScript Upload & Download row
= sheet.createRow((short) j);
JavaScript Upload & Download
//設置i列從第二列開始
JavaScript Upload & DownloadJavaScript Upload & Download
for (int i =1; i <=3; i++) JavaScript Upload & Download{
JavaScript Upload & Download HSSFCell cell
= row.createCell((short) (i-1));
JavaScript Upload & Download
// 設置單元格格式
JavaScript Upload & Download
cell.setCellStyle(cellStyle);
JavaScript Upload & Download cell.setCellType(HSSFCell.CELL_TYPE_STRING);
JavaScript Upload & Download cell.setEncoding(HSSFCell.ENCODING_UTF_16);
JavaScript Upload & Download cell.setCellValue(rs.getString(i));
JavaScript Upload & Download }

JavaScript Upload & Download
JavaScript Upload & Download j
++;
JavaScript Upload & Download }

JavaScript Upload & Download
JavaScript Upload & Download request.setAttribute(
"message", "文件生成成功!");
JavaScript Upload & DownloadJavaScript Upload & Download }
catch (SQLException e) JavaScript Upload & Download{
JavaScript Upload & Download request.setAttribute(
"message", "創建文件失敗!");
JavaScript Upload & Download e.printStackTrace();
JavaScript Upload & Download }

JavaScript Upload & Download
// 刪除路徑下同名的Excel 文件
JavaScript Upload & Download
File path =new File(outputFile);
JavaScript Upload & Download path.delete();
JavaScript Upload & Download
JavaScript Upload & Download
// 新建一輸出文件流
JavaScript Upload & Download
FileOutputStream fOut =new FileOutputStream(outputFile);
JavaScript Upload & Download
// 把相應的Excel 工作簿存盤
JavaScript Upload & Download
workbook.write(fOut);
JavaScript Upload & Download
// 操作結束,關閉文件
JavaScript Upload & Download
fOut.flush();
JavaScript Upload & Download fOut.close();
JavaScript Upload & Download
//該處如果Excel過大會影響效率,誰有好的想法可以提出來參考(不過從頁面下載完后就會清空)
JavaScript Upload & Download
request.getSession().setAttribute("Download", outputFile);
JavaScript Upload & Download
JavaScript Upload & DownloadJavaScript Upload & Download }
catch (Exception ioexception) JavaScript Upload & Download{
JavaScript Upload & Download request.setAttribute(
"message", "創建文件失敗!");
JavaScript Upload & Download
return actionMapping.findForward("outJSP");
JavaScript Upload & Download }

JavaScript Upload & Download
JavaScript Upload & Download
return actionMapping.findForward("outJSP");
JavaScript Upload & Download }

JavaScript Upload & Download
JavaScript Upload & DownloadJavaScript Upload & Download
/**//*
JavaScript Upload & Download * 從Excel文件中讀取數據,并導入到數據庫中
JavaScript Upload & Download *
*/

JavaScript Upload & Download
public ActionForward getUpload(ActionMapping actionMapping,
JavaScript Upload & Download ActionForm actionForm, HttpServletRequest request,
JavaScript Upload & DownloadJavaScript Upload & Download HttpServletResponse response)
throws Exception JavaScript Upload & Download{
JavaScript Upload & Download
// 獲取excel 文件
JavaScript Upload & Download
Form fm = (Form) actionForm;
JavaScript Upload & Download FormFile formfile
= fm.getUploadfile();
JavaScript Upload & Download InputStream inputstream
= formfile.getInputStream();
JavaScript Upload & Download fm.clear();
// 清空
JavaScript Upload & Download
Session session = HibernateSession.currentSession();
JavaScript Upload & Download ArrayList list
=new ArrayList();
JavaScript Upload & Download
int input =0; //導入記數
JavaScript Upload & Download
String name =null;
JavaScript Upload & Download String sex
=null;
JavaScript Upload & Download String age
=null;
JavaScript Upload & Download
JavaScript Upload & DownloadJavaScript Upload & Download
tryJavaScript Upload & Download{
JavaScript Upload & Download
//通過得到的文件輸入流inputstream創建一個HSSFWordbook對象
JavaScript Upload & Download
HSSFWorkbook hssfworkbook =new HSSFWorkbook(inputstream);
JavaScript Upload & Download HSSFSheet hssfsheet
= hssfworkbook.getSheetAt(0);//第一個工作表
JavaScript Upload & Download
HSSFRow hssfrow = hssfsheet.getRow(0);//第一行
JavaScript Upload & Download
JavaScript Upload & Download
//遍歷該表格中所有的工作表,i表示工作表的數量 getNumberOfSheets表示工作表的總數
JavaScript Upload & DownloadJavaScript Upload & Download
for (int i =0; i < hssfworkbook.getNumberOfSheets(); i++) JavaScript Upload & Download{
JavaScript Upload & Download hssfsheet
= hssfworkbook.getSheetAt(i);
JavaScript Upload & Download
JavaScript Upload & Download
//遍歷該行所有的行,j表示行數 getPhysicalNumberOfRows行的總數
JavaScript Upload & DownloadJavaScript Upload & Download
for (int j =1; j < hssfsheet.getPhysicalNumberOfRows(); j++) JavaScript Upload & Download{
JavaScript Upload & Download hssfrow
= hssfsheet.getRow(j);
JavaScript Upload & Download
//判斷是否還存在需要導入的數據
JavaScript Upload & DownloadJavaScript Upload & Download
if (hssfrow ==null) JavaScript Upload & Download{
JavaScript Upload & Download System.out.println(
"這里已沒有數據,在第"+i+"列,第"+j+"");
JavaScript Upload & Download
break;
JavaScript Upload & Download }

JavaScript Upload & DownloadJavaScript Upload & Download
/** *//**將EXCEL中的第 j 行,第一列的值插入到實例中*/
JavaScript Upload & DownloadJavaScript Upload & Download
if (hssfrow.getCell((short) 0) ==null) JavaScript Upload & Download{
JavaScript Upload & Download name
="";
JavaScript Upload & DownloadJavaScript Upload & Download }
elseif (hssfrow.getCell((short) 0).getCellType() ==0) JavaScript Upload & Download{
JavaScript Upload & Download name
=new Double(hssfrow.getCell((short) 0).getNumericCellValue()).toString();
JavaScript Upload & Download }

JavaScript Upload & Download
//如果EXCEL表格中的數據類型為字符串型
JavaScript Upload & DownloadJavaScript Upload & Download
elseJavaScript Upload & Download{
JavaScript Upload & Download name
= hssfrow.getCell((short) 0).getStringCellValue().trim();
JavaScript Upload & Download }

JavaScript Upload & DownloadJavaScript Upload & Download
/** *//**將EXCEL中的第 j 行,第二列的值插入到實例中*/
JavaScript Upload & Download
//姓名
JavaScript Upload & DownloadJavaScript Upload & Download
if(hssfrow.getCell((short) 1) ==null)JavaScript Upload & Download{
JavaScript Upload & Download sex
="";
JavaScript Upload & DownloadJavaScript Upload & Download }
elseif(hssfrow.getCell((short) 1).getCellType() ==0) JavaScript Upload & Download{
JavaScript Upload & Download sex
=new Double(hssfrow.getCell((short) 1).getNumericCellValue()).toString();
JavaScript Upload & Download }

JavaScript Upload & Download
//如果EXCEL表格中的數據類型為字符串型
JavaScript Upload & DownloadJavaScript Upload & Download
elseJavaScript Upload & Download{
JavaScript Upload & Download sex
= hssfrow.getCell((short) 1).getStringCellValue().trim();
JavaScript Upload & Download }

JavaScript Upload & DownloadJavaScript Upload & Download
/** *//**將EXCEL中的第 j 行,第三列的值插入到實例中*/
JavaScript Upload & Download
//姓名
JavaScript Upload & DownloadJavaScript Upload & Download
if(hssfrow.getCell((short) 1) ==null)JavaScript Upload & Download{
JavaScript Upload & Download age
="";
JavaScript Upload & DownloadJavaScript Upload & Download }
elseif(hssfrow.getCell((short) 1).getCellType() ==0) JavaScript Upload & Download{
JavaScript Upload & Download
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

彭阳县| 夹江县| 阳江市| 城步| 广河县| 红原县| 鹤峰县| 怀宁县| 历史| 灵石县| 祁东县| 吉木乃县| 宜良县| 栖霞市| 项城市| 克拉玛依市| 陈巴尔虎旗| 都昌县| 石狮市| 高安市| 望江县| 宁德市| 景德镇市| 修武县| 连云港市| 福清市| 鲜城| 保山市| 明水县| 通辽市| 汉阴县| 光泽县| 泾川县| 寿宁县| 吉木萨尔县| 中江县| 铁力市| 栾川县| 桃园县| 鄱阳县| 韩城市|