- 浏览: 264193 次
- 性别:
- 来自: 长沙
文章分类
最新评论
-
ae6623:
楼主,没必要用json2.js,jquery自己就可以去解析j ...
JQuery、JSON、Ajax在Servlet中的应用 -
hw1287789687:
http://ljhzzyx.blog.163.com/blo ...
MyEclipse中如何去掉JS/JSP 语法错误提示 -
xiaokang1582830:
不知所云...
处理 Struts2 框架上传的文件 -
halfwaters:
使用vs2010,加上扩展管理器安装的js插件
MyEclipse中如何去掉JS/JSP 语法错误提示 -
ayumi11111:
附件呢 我····
jQuery+Ajax+Struts2. js javascript
这一段时间搞报表,头都搞晕了,最后还是整理了一下
在javascript里面导出Excel
这个是基本方法
function toExcel(){
var docWin=window.open("","_blank","left=-1,top=-1,width=100,height=100,resizable=no");
var AllData=document.getElementById("AllData");//"alldata"为一个表格或一个层(最好)的名字
docWin.document.write(AllData.innerHTML);
docWin.document.execCommand("SelectAll",false);
docWin.document.execCommand("Copy",false);
docWin.close();
var objExcelApp=new ActiveXObject("Excel.Application");
if (objExcelApp==null){
alert("你的机器上没有安装Excel软件");
}else{
var erl="";
try{
objExcelApp.WindowState = 1;
objExcelApp.Visible = 1;
var objWorkBook=objExcelApp.Workbooks.Add();
erl="Create WorkBook";
var objSheet=objWorkBook.ActiveSheet;
erl="Get ActiveSheet";
objSheet.Columns("A").columnwidth="5";
objSheet.Columns("B").columnwidth="5";
objSheet.Columns("C").columnwidth="15";
objSheet.Columns("D").columnwidth="15";
objSheet.Columns("E").columnwidth="35";
objSheet.Columns("F").columnwidth="15";
objSheet.Columns("G").columnwidth="5";
objSheet.Columns("H").columnwidth="25";
objSheet.Paste();
erl="Copy";
// 保存表格。
//objSheet.SaveAs("C:\\TEST.XLS");
// 用 Application 对象用 Quit 方法关闭 Excel。
/objSheet.Application.Quit();
}catch(e){
alert("对Excel操作时出错了:"+erl);
}
}
}
objSheet.cells.rang['A3','B3'].merge('true');可以合并两列
objSheet.cells.rang['A3','B3'].merge(False);可以合并两行
下面这些可以对Excel对象的属性进行操作
1.创建一个新Excel表格
var XLObj = new ActiveXObject("Excel.Application");
var xlBook = XLObj.Workbooks.Add; //新增工作簿
var ExcelSheet = xlBook.Worksheets(1); //创建工作表
2.保存表格
ExcelSheet.SaveAs("C:\\TEST.XLS");
3.使 Excel 通过 Application 对象可见
ExcelSheet.Application.Visible = true;
4.打印
xlBook.PrintOut;
或者:
ExcelSheet.PrintOut;
5.关闭
xlBook.Close(savechanges=false);
或者:
ExcelSheet.Close(savechanges=false);
6.结束进程
ExcelSheet.Application.Quit();
或者:
XLObj.Quit();
XLObj=null;
7.页面设置
ExcelSheet.ActiveSheet.PageSetup.LeftMargin= 2/0.035; //页边距 左2厘米
ExcelSheet.ActiveSheet.PageSetup.RightMargin = 3/0.035; //页边距 右3厘米,
ExcelSheet.ActiveSheet.PageSetup.TopMargin = 4/0.035; //页边距 上4厘米,
ExcelSheet.ActiveSheet.PageSetup.BottomMargin = 5/0.035; //页边距 下5厘米
ExcelSheet.ActiveSheet.PageSetup.HeaderMargin = 1/0.035; //页边距 页眉1厘米
ExcelSheet.ActiveSheet.PageSetup.FooterMargin = 2/0.035; //页边距 页脚2厘米
ExcelSheet.ActiveSheet.PageSetup.CenterHeader = "页眉中部内容";
ExcelSheet.ActiveSheet.PageSetup.LeftHeader = "页眉左部内容";
ExcelSheet.ActiveSheet.PageSetup.RightHeader = "页眉右部内容";
ExcelSheet.ActiveSheet.PageSetup.CenterFooter = "页脚中部内容";
ExcelSheet.ActiveSheet.PageSetup.LeftFooter = "页脚左部内容";
ExcelSheet.ActiveSheet.PageSetup.RightFooter = "页脚右部内容";
8.对单元格操作,带*部分对于行,列,区域都有相应属性
ExcelSheet.ActiveSheet.Cells(row,col).Value = "内容"; //设置单元格内容
ExcelSheet.ActiveSheet.Cells(row,col).Borders.Weight = 1; //设置单元格边框*()
ExcelSheet.ActiveSheet.Cells(row,col).Interior.ColorIndex = 1; //设置单元格底色*(1-黑色,
2-白色,3-红色,4-绿色,5-蓝色,6-黄色,7-粉红色,8-天蓝色,9-酱土色..可以多做尝试)
ExcelSheet.ActiveSheet.Cells(row,col).Interior.Pattern = 1; //设置单元格背景样式*(1-无,
2-细网格,3-粗网格,4-斑点,5-横线,6-竖线..可以多做尝试)
ExcelSheet.ActiveSheet.Cells(row,col).Font.ColorIndex = 1; //设置字体颜色*(与上相同)
ExcelSheet.ActiveSheet.Cells(row,col).Font.Size = 10; //设置为10号字*
ExcelSheet.ActiveSheet.Cells(row,col).Font.Name = "黑体"; //设置为黑体*
ExcelSheet.ActiveSheet.Cells(row,col).Font.Italic = true; //设置为斜体*
ExcelSheet.ActiveSheet.Cells(row,col).Font.Bold = true; //设置为粗体*
ExcelSheet.ActiveSheet.Cells(row,col).ClearContents; //清除内容*
ExcelSheet.ActiveSheet.Cells(row,col).WrapText=true; //设置为自动换行*
ExcelSheet.ActiveSheet.Cells(row,col).HorizontalAlignment = 3; //水平对齐方式枚举* (1-常规,
2-靠左,3-居中,4-靠右,5-填充 6-两端对齐,7-跨列居中,8-分散对齐)
ExcelSheet.ActiveSheet.Cells(row,col).VerticalAlignment = 2; //垂直对齐方式枚举*(1-靠上,
2-居中,3-靠下,4-两端对齐,5-分散对齐)
//行,列有相应操作:
ExcelSheet.ActiveSheet.Rows(row).
ExcelSheet.ActiveSheet.Columns(col).
ExcelSheet.ActiveSheet.Rows(startrow+":"+endrow). //如Rows("1:5")即1到5行
ExcelSheet.ActiveSheet.Columns(startcol+":"+endcol). //如Columns("1:5")即1到5列
//区域有相应操作:
XLObj.Range(startcell+":"+endcell).Select;
//如Range("A2:H8")即A列第2格至H列第8格的整个区域
XLObj.Selection.
//合并单元格
XLObj.Range(startcell+":"+endcell).MergeCells = true;
//如Range("A2:H8")即将A列第2格至H列第8格的整个区域合并为一个单元格
或者:
XLObj.Range("A2",XLObj.Cells(8,).MergeCells = true;
9.设置行高与列宽
ExcelSheet.ActiveSheet.Columns(startcol+":"+endcol).ColumnWidth = 22;
//设置从firstcol到stopcol列的宽度为22
ExcelSheet.ActiveSheet.Rows(startrow+":"+endrow).RowHeight = 22;
//设置从firstrow到stoprow行的宽度为22
这里有一个在网上搜索的小实例。
<HTML>
<BODY>
<SCRIPT LANGUAGE="VBScript">
Function CreateNamesArray()
' Create an array to set multiple values at once.
Dim saNames(5, 2)
saNames(0, 0) = "John"
saNames(0, 1) = "Smith"
saNames(1, 0) = "Tom"
saNames(1, 1) = "Brown"
saNames(2, 0) = "Sue"
saNames(2, 1) = "Thomas"
saNames(3, 0) = "Jane"
saNames(3, 1) = "Jones"
saNames(4, 0) = "Adam"
saNames(4, 1) = "Johnson"
CreateNamesArray = saNames
End Function
</SCRIPT>
<SCRIPT LANGUAGE="JScript">
function AutomateExcel()
{
// Start Excel and get Application object.
var oXL = new ActiveXObject("Excel.Application");
oXL.Visible = true;
// Get a new workbook.
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
// Add table headers going cell by cell.
oSheet.Cells(1, 1).Value = "First Name";
oSheet.Cells(1, 2).Value = "Last Name";
oSheet.Cells(1, 3).Value = "Full Name";
oSheet.Cells(1, 4).Value = "Salary";
// Format A1:D1 as bold, vertical alignment = center.
oSheet.Range("A1", "D1").Font.Bold = true;
oSheet.Range("A1", "D1").VerticalAlignment = -4108; //xlVAlignCenter
// Create an array to set multiple values at once.
// Fill A2:B6 with an array of values (from VBScript).
oSheet.Range("A2", "B6").Value = CreateNamesArray();
// Fill C2:C6 with a relative formula (=A2 & " " & B2).
var oRng = oSheet.Range("C2", "C6");
oRng.Formula = "=A2 & \" \" & B2";
// Fill D2:D6 with a formula(=RAND()*100000) and apply format.
oRng = oSheet.Range("D2", "D6");
oRng.Formula = "=RAND()*100000";
oRng.NumberFormat = "$0.00";
// AutoFit columns A:D.
oRng = oSheet.Range("A1", "D1");
oRng.EntireColumn.AutoFit();
// Manipulate a variable number of columns for Quarterly Sales Data.
DispalyQuarterlySales(oSheet);
//保存到本地路径
oSheet.SaveAs("C:\\TEST.XLS");
// Make sure Excel is visible and give the user control
// of Excel's lifetime.
oXL.Visible = true;
oXL.UserControl = true;
}
function DispalyQuarterlySales(oWS)
{
var iNumQtrs, sMsg, iRet;
// Number of quarters to display data for.
iNumQtrs = 4;
// Starting at E1, fill headers for the number of columns selected.
var oResizeRange = oWS.Range("E1", "E1").Resize(1,iNumQtrs);
oResizeRange.Formula = "=\"Q\" & COLUMN()-4 & CHAR(10) & \"Sales\"";
// Change the Orientation and WrapText properties for the headers.
oResizeRange.Orientation = 38;
oResizeRange.WrapText = true;
// Fill the interior color of the headers.
oResizeRange.Interior.ColorIndex = 36;
// Fill the columns with a formula and apply a number format.
oResizeRange = oWS.Range("E2", "E6").Resize(5,iNumQtrs);
oResizeRange.Formula = "=RAND()*100";
oResizeRange.NumberFormat = "$0.00";
// Apply borders to the Sales data and headers.
oResizeRange = oWS.Range("E1", "E6").Resize(6,iNumQtrs);
oResizeRange.Borders.Weight = 2; // xlThin
// Add a Totals formula for the sales data and apply a border.
oResizeRange = oWS.Range("E8", "E8").Resize(1,iNumQtrs);
oResizeRange.Formula = "=SUM(E2:E6)";
// 9 = xlEdgeBottom
oResizeRange.Borders(9).LineStyle = -4119; //xlDouble
oResizeRange.Borders(9).Weight = 4; //xlThick
// Add a Chart for the selected data.
oResizeRange = oWS.Range("E2:E6").Resize(5,iNumQtrs);
var oChart = oWS.Parent.Charts.Add();
oChart.ChartWizard(oResizeRange, -4100, null, 2); // -4100 = xl3dColumn
oChart.SeriesCollection(1).XValues = oWS.Range("A2", "A6");
for (iRet = 1; iRet <= iNumQtrs; iRet++) {
oChart.SeriesCollection(iRet).Name = "=\"Q" + iRet + "\"";
}
oChart.Location(2, oWS.Name); // 2 = xlLocationAsObject
// Move the chart so as not to cover your data.
oWS.Shapes("Chart 1").Top = oWS.Rows(10).Top;
oWS.Shapes("Chart 1").Left = oWS.Columns(2).Left;
}
</SCRIPT>
<P><INPUT id=button1 type=button value="创建Excel表格和柱状图" onclick="AutomateExcel"></P>
</BODY>
</HTML>
这里是在java类里面的方法
public void generateXls()
{
try
{
/** *//** **********创建工作簿************ */
WritableWorkbook workbook = Workbook.createWorkbook(new File("d:/test.xls"));
/** *//** **********创建工作表************ */
WritableSheet sheet = workbook.createSheet("工作表名称", 0);
/** *//** *********设置列宽**************** */
sheet.setColumnView(0, 15); // 第1列
sheet.setColumnView(1, 18); // 第2列
sheet.setColumnView(2, 13);
sheet.setColumnView(3, 13);
sheet.setColumnView(4, 15);
sheet.setColumnView(5, 15);
//设置行高
sheet.setRowView(0, 600, false);
sheet.setRowView(1, 400, false);
sheet.setRowView(7, 400, false);
//设置页边距
sheet.getSettings().setRightMargin(0.5);
//设置页脚
sheet.setFooter("", "", "测试页脚");
/** *//** ************设置单元格字体************** */
//字体
WritableFont NormalFont = new WritableFont(WritableFont.ARIAL, 10);
WritableFont BoldFont = new WritableFont(WritableFont.ARIAL, 14,
WritableFont.BOLD);
WritableFont tableFont = new WritableFont(WritableFont.ARIAL, 12,
WritableFont.NO_BOLD);
WritableFont baodanFont = new WritableFont(WritableFont.ARIAL, 10,
WritableFont.BOLD);
/** *//** ************以下设置几种格式的单元格************ */
// 用于标题
WritableCellFormat wcf_title = new WritableCellFormat(BoldFont);
wcf_title.setBorder(Border.NONE, BorderLineStyle.THIN); // 线条
wcf_title.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐
wcf_title.setAlignment(Alignment.CENTRE); // 水平对齐
wcf_title.setWrap(true); // 是否换行
// 用于表格标题
WritableCellFormat wcf_tabletitle = new WritableCellFormat(
tableFont);
wcf_tabletitle.setBorder(Border.NONE, BorderLineStyle.THIN); // 线条
wcf_tabletitle.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐
wcf_tabletitle.setAlignment(Alignment.CENTRE); // 水平对齐
wcf_tabletitle.setWrap(true); // 是否换行
// 用于正文左
WritableCellFormat wcf_left = new WritableCellFormat(NormalFont);
wcf_left.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
wcf_left.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐
wcf_left.setAlignment(Alignment.LEFT);
wcf_left.setWrap(true); // 是否换行
// 用于正文左
WritableCellFormat wcf_center = new WritableCellFormat(NormalFont);
wcf_center.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐
wcf_center.setAlignment(Alignment.CENTRE);
wcf_center.setWrap(true); // 是否换行
// 用于正文右
WritableCellFormat wcf_right = new WritableCellFormat(NormalFont);
wcf_right.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
wcf_right.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐
wcf_right.setAlignment(Alignment.RIGHT);
wcf_right.setWrap(false); // 是否换行
// 用于跨行
WritableCellFormat wcf_merge = new WritableCellFormat(NormalFont);
wcf_merge.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
wcf_merge.setVerticalAlignment(VerticalAlignment.TOP); // 垂直对齐
wcf_merge.setAlignment(Alignment.LEFT);
wcf_merge.setWrap(true); // 是否换行
WritableCellFormat wcf_table = new WritableCellFormat(NormalFont);
wcf_table.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
wcf_table.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐
wcf_table.setAlignment(Alignment.CENTRE);
wcf_table.setBackground(Colour.GRAY_25);
wcf_table.setWrap(true); // 是否换行
/** *//** ************单元格格式设置完成****************** */
//合并单元格,注意mergeCells(col0,row0,col1,row1) --列从0开始,col1为你要合并到第几列,行也一样
sheet.mergeCells(0, 0, 5, 0);
sheet.addCell(new Label(0, 0, "这里是大标题,自定义格式",
wcf_title));
sheet.mergeCells(0, 1, 1, 1);
sheet.mergeCells(2, 1, 5, 1);
sheet.addCell(new Label(0, 1, "", wcf_center));
sheet.addCell(new Label(2, 1, "姓名:" + "supercrsky",
wcf_center));
sheet.mergeCells(0, 2, 1, 2);
sheet.mergeCells(2, 2, 3, 2);
sheet.addCell(new Label(0, 2, "单位:", wcf_center));
sheet.addCell(new Label(2, 2, "ChinaLong", wcf_center));
sheet.addCell(new Label(4, 2, "薪水", wcf_center));
sheet.addCell(new Label(5, 2, "5000", wcf_center));
sheet.mergeCells(0, 3, 1, 3);
sheet.mergeCells(2, 3, 3, 3);
sheet.addCell(new Label(0, 3, "性别:", wcf_center));
sheet.addCell(new Label(2, 3, "男", wcf_center));
sheet.addCell(new Label(4, 3, "婚否:", wcf_center));
sheet.addCell(new Label(5, 3, "否", wcf_center));
sheet.mergeCells(0, 4, 1, 4);
sheet.mergeCells(2, 4, 3, 4);
sheet.addCell(new Label(0, 4, "是否在职:", wcf_center));
sheet.addCell(new Label(2, 4,"是",
wcf_center));
sheet.addCell(new Label(4, 4,"工作经验:", wcf_center));
sheet.addCell(new Label(5, 4, "4",wcf_center));
sheet.mergeCells(0, 5, 1, 5);
sheet.mergeCells(2, 5, 3, 5);
sheet.addCell(new Label(0, 5, "保险费:", wcf_center));
sheet.addCell(new Label(2, 5,"50",
wcf_center));
sheet.addCell(new Label(4, 5, "保险金额:", wcf_center));
sheet.addCell(new Label(5, 5, "50000",
wcf_center));
sheet.mergeCells(0, 6, 1, 6);
sheet.mergeCells(2, 6, 3, 6);
sheet.addCell(new Label(0, 6, "工作地点:", wcf_center));
sheet.addCell(new Label(2, 6, "北京", wcf_center));
sheet.addCell(new Label(4, 6, "开心度:", wcf_center));
sheet.addCell(new Label(5, 6, "一般", wcf_center));
// 另起一table
sheet.mergeCells(0, 7, 5, 7);
sheet.addCell(new Label(0, 7, "详细数据", wcf_tabletitle));
// table标题
sheet.addCell(new Label(0, 8, "序号", wcf_table));
sheet.addCell(new Label(1, 8, "姓名", wcf_table));
sheet.addCell(new Label(2, 8, "年龄", wcf_table));
sheet.addCell(new Label(3, 8, "性别", wcf_table));
sheet.addCell(new Label(4, 8, "婚否", wcf_table));
sheet.addCell(new Label(5, 8, "在职", wcf_table));
// table内容
//这里用你的dao
TestDAO dao = new TestDAO();
List list = dao.findBy(user.getUserId());
System.out.println("此保单拥有防疫码数量:" + list.size());
for (int i = 0; i < list.size(); i++)
{
//对应你的vo类
User data = (User) list.get(i);
sheet.addCell(new Label(0, 9 + i, String.valueOf(i + 1),
wcf_center));
sheet.addCell(new Label(1, 9 + i, data.getDlEPCode(),
wcf_center));
sheet
.addCell(new Label(2, 9 + i, data.getDlType(),
wcf_center));
sheet.addCell(new Label(3, 9 + i, String.valueOf(data
.getDlPigAge()), wcf_center));
sheet.addCell(new Label(4, 9 + i, "", wcf_center));
sheet.addCell(new Label(5, 9 + i, "", wcf_center));
}
/** *//** **********以上所写的内容都是写在缓存中的,下一句将缓存的内容写到文件中******** */
workbook.write();
/** *//** *********关闭文件************* */
workbook.close();
System.out.println("导出成功");
// 存放url地址
} catch (Exception e)
{
System.out.println("在输出到EXCEL的过程中出现错误,错误原因:" + e.toString());
}
}
外加一段读取流的代码: 呵呵非常有用哦!!
HttpServletResponse response = ServletActionContext.getResponse();
String fileChName="项目积分综合报表.xls";
response.reset();
response.setCharacterEncoding("ISO8859-1");
response.setHeader("Content-Disposition", "attachment; filename="
+ new String(fileChName.getBytes("GBK"), "ISO8859-1"));//fileChName为下载时用户看到的文件名
response.setHeader("Connection", "close");
response.setHeader("Content-Type", "application/octet-stream");
OutputStream out = response.getOutputStream();
WritableWorkbook wwb = Workbook.createWorkbook(out);
在javascript里面导出Excel
这个是基本方法
function toExcel(){
var docWin=window.open("","_blank","left=-1,top=-1,width=100,height=100,resizable=no");
var AllData=document.getElementById("AllData");//"alldata"为一个表格或一个层(最好)的名字
docWin.document.write(AllData.innerHTML);
docWin.document.execCommand("SelectAll",false);
docWin.document.execCommand("Copy",false);
docWin.close();
var objExcelApp=new ActiveXObject("Excel.Application");
if (objExcelApp==null){
alert("你的机器上没有安装Excel软件");
}else{
var erl="";
try{
objExcelApp.WindowState = 1;
objExcelApp.Visible = 1;
var objWorkBook=objExcelApp.Workbooks.Add();
erl="Create WorkBook";
var objSheet=objWorkBook.ActiveSheet;
erl="Get ActiveSheet";
objSheet.Columns("A").columnwidth="5";
objSheet.Columns("B").columnwidth="5";
objSheet.Columns("C").columnwidth="15";
objSheet.Columns("D").columnwidth="15";
objSheet.Columns("E").columnwidth="35";
objSheet.Columns("F").columnwidth="15";
objSheet.Columns("G").columnwidth="5";
objSheet.Columns("H").columnwidth="25";
objSheet.Paste();
erl="Copy";
// 保存表格。
//objSheet.SaveAs("C:\\TEST.XLS");
// 用 Application 对象用 Quit 方法关闭 Excel。
/objSheet.Application.Quit();
}catch(e){
alert("对Excel操作时出错了:"+erl);
}
}
}
objSheet.cells.rang['A3','B3'].merge('true');可以合并两列
objSheet.cells.rang['A3','B3'].merge(False);可以合并两行
下面这些可以对Excel对象的属性进行操作
1.创建一个新Excel表格
var XLObj = new ActiveXObject("Excel.Application");
var xlBook = XLObj.Workbooks.Add; //新增工作簿
var ExcelSheet = xlBook.Worksheets(1); //创建工作表
2.保存表格
ExcelSheet.SaveAs("C:\\TEST.XLS");
3.使 Excel 通过 Application 对象可见
ExcelSheet.Application.Visible = true;
4.打印
xlBook.PrintOut;
或者:
ExcelSheet.PrintOut;
5.关闭
xlBook.Close(savechanges=false);
或者:
ExcelSheet.Close(savechanges=false);
6.结束进程
ExcelSheet.Application.Quit();
或者:
XLObj.Quit();
XLObj=null;
7.页面设置
ExcelSheet.ActiveSheet.PageSetup.LeftMargin= 2/0.035; //页边距 左2厘米
ExcelSheet.ActiveSheet.PageSetup.RightMargin = 3/0.035; //页边距 右3厘米,
ExcelSheet.ActiveSheet.PageSetup.TopMargin = 4/0.035; //页边距 上4厘米,
ExcelSheet.ActiveSheet.PageSetup.BottomMargin = 5/0.035; //页边距 下5厘米
ExcelSheet.ActiveSheet.PageSetup.HeaderMargin = 1/0.035; //页边距 页眉1厘米
ExcelSheet.ActiveSheet.PageSetup.FooterMargin = 2/0.035; //页边距 页脚2厘米
ExcelSheet.ActiveSheet.PageSetup.CenterHeader = "页眉中部内容";
ExcelSheet.ActiveSheet.PageSetup.LeftHeader = "页眉左部内容";
ExcelSheet.ActiveSheet.PageSetup.RightHeader = "页眉右部内容";
ExcelSheet.ActiveSheet.PageSetup.CenterFooter = "页脚中部内容";
ExcelSheet.ActiveSheet.PageSetup.LeftFooter = "页脚左部内容";
ExcelSheet.ActiveSheet.PageSetup.RightFooter = "页脚右部内容";
8.对单元格操作,带*部分对于行,列,区域都有相应属性
ExcelSheet.ActiveSheet.Cells(row,col).Value = "内容"; //设置单元格内容
ExcelSheet.ActiveSheet.Cells(row,col).Borders.Weight = 1; //设置单元格边框*()
ExcelSheet.ActiveSheet.Cells(row,col).Interior.ColorIndex = 1; //设置单元格底色*(1-黑色,
2-白色,3-红色,4-绿色,5-蓝色,6-黄色,7-粉红色,8-天蓝色,9-酱土色..可以多做尝试)
ExcelSheet.ActiveSheet.Cells(row,col).Interior.Pattern = 1; //设置单元格背景样式*(1-无,
2-细网格,3-粗网格,4-斑点,5-横线,6-竖线..可以多做尝试)
ExcelSheet.ActiveSheet.Cells(row,col).Font.ColorIndex = 1; //设置字体颜色*(与上相同)
ExcelSheet.ActiveSheet.Cells(row,col).Font.Size = 10; //设置为10号字*
ExcelSheet.ActiveSheet.Cells(row,col).Font.Name = "黑体"; //设置为黑体*
ExcelSheet.ActiveSheet.Cells(row,col).Font.Italic = true; //设置为斜体*
ExcelSheet.ActiveSheet.Cells(row,col).Font.Bold = true; //设置为粗体*
ExcelSheet.ActiveSheet.Cells(row,col).ClearContents; //清除内容*
ExcelSheet.ActiveSheet.Cells(row,col).WrapText=true; //设置为自动换行*
ExcelSheet.ActiveSheet.Cells(row,col).HorizontalAlignment = 3; //水平对齐方式枚举* (1-常规,
2-靠左,3-居中,4-靠右,5-填充 6-两端对齐,7-跨列居中,8-分散对齐)
ExcelSheet.ActiveSheet.Cells(row,col).VerticalAlignment = 2; //垂直对齐方式枚举*(1-靠上,
2-居中,3-靠下,4-两端对齐,5-分散对齐)
//行,列有相应操作:
ExcelSheet.ActiveSheet.Rows(row).
ExcelSheet.ActiveSheet.Columns(col).
ExcelSheet.ActiveSheet.Rows(startrow+":"+endrow). //如Rows("1:5")即1到5行
ExcelSheet.ActiveSheet.Columns(startcol+":"+endcol). //如Columns("1:5")即1到5列
//区域有相应操作:
XLObj.Range(startcell+":"+endcell).Select;
//如Range("A2:H8")即A列第2格至H列第8格的整个区域
XLObj.Selection.
//合并单元格
XLObj.Range(startcell+":"+endcell).MergeCells = true;
//如Range("A2:H8")即将A列第2格至H列第8格的整个区域合并为一个单元格
或者:
XLObj.Range("A2",XLObj.Cells(8,).MergeCells = true;
9.设置行高与列宽
ExcelSheet.ActiveSheet.Columns(startcol+":"+endcol).ColumnWidth = 22;
//设置从firstcol到stopcol列的宽度为22
ExcelSheet.ActiveSheet.Rows(startrow+":"+endrow).RowHeight = 22;
//设置从firstrow到stoprow行的宽度为22
这里有一个在网上搜索的小实例。
<HTML>
<BODY>
<SCRIPT LANGUAGE="VBScript">
Function CreateNamesArray()
' Create an array to set multiple values at once.
Dim saNames(5, 2)
saNames(0, 0) = "John"
saNames(0, 1) = "Smith"
saNames(1, 0) = "Tom"
saNames(1, 1) = "Brown"
saNames(2, 0) = "Sue"
saNames(2, 1) = "Thomas"
saNames(3, 0) = "Jane"
saNames(3, 1) = "Jones"
saNames(4, 0) = "Adam"
saNames(4, 1) = "Johnson"
CreateNamesArray = saNames
End Function
</SCRIPT>
<SCRIPT LANGUAGE="JScript">
function AutomateExcel()
{
// Start Excel and get Application object.
var oXL = new ActiveXObject("Excel.Application");
oXL.Visible = true;
// Get a new workbook.
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
// Add table headers going cell by cell.
oSheet.Cells(1, 1).Value = "First Name";
oSheet.Cells(1, 2).Value = "Last Name";
oSheet.Cells(1, 3).Value = "Full Name";
oSheet.Cells(1, 4).Value = "Salary";
// Format A1:D1 as bold, vertical alignment = center.
oSheet.Range("A1", "D1").Font.Bold = true;
oSheet.Range("A1", "D1").VerticalAlignment = -4108; //xlVAlignCenter
// Create an array to set multiple values at once.
// Fill A2:B6 with an array of values (from VBScript).
oSheet.Range("A2", "B6").Value = CreateNamesArray();
// Fill C2:C6 with a relative formula (=A2 & " " & B2).
var oRng = oSheet.Range("C2", "C6");
oRng.Formula = "=A2 & \" \" & B2";
// Fill D2:D6 with a formula(=RAND()*100000) and apply format.
oRng = oSheet.Range("D2", "D6");
oRng.Formula = "=RAND()*100000";
oRng.NumberFormat = "$0.00";
// AutoFit columns A:D.
oRng = oSheet.Range("A1", "D1");
oRng.EntireColumn.AutoFit();
// Manipulate a variable number of columns for Quarterly Sales Data.
DispalyQuarterlySales(oSheet);
//保存到本地路径
oSheet.SaveAs("C:\\TEST.XLS");
// Make sure Excel is visible and give the user control
// of Excel's lifetime.
oXL.Visible = true;
oXL.UserControl = true;
}
function DispalyQuarterlySales(oWS)
{
var iNumQtrs, sMsg, iRet;
// Number of quarters to display data for.
iNumQtrs = 4;
// Starting at E1, fill headers for the number of columns selected.
var oResizeRange = oWS.Range("E1", "E1").Resize(1,iNumQtrs);
oResizeRange.Formula = "=\"Q\" & COLUMN()-4 & CHAR(10) & \"Sales\"";
// Change the Orientation and WrapText properties for the headers.
oResizeRange.Orientation = 38;
oResizeRange.WrapText = true;
// Fill the interior color of the headers.
oResizeRange.Interior.ColorIndex = 36;
// Fill the columns with a formula and apply a number format.
oResizeRange = oWS.Range("E2", "E6").Resize(5,iNumQtrs);
oResizeRange.Formula = "=RAND()*100";
oResizeRange.NumberFormat = "$0.00";
// Apply borders to the Sales data and headers.
oResizeRange = oWS.Range("E1", "E6").Resize(6,iNumQtrs);
oResizeRange.Borders.Weight = 2; // xlThin
// Add a Totals formula for the sales data and apply a border.
oResizeRange = oWS.Range("E8", "E8").Resize(1,iNumQtrs);
oResizeRange.Formula = "=SUM(E2:E6)";
// 9 = xlEdgeBottom
oResizeRange.Borders(9).LineStyle = -4119; //xlDouble
oResizeRange.Borders(9).Weight = 4; //xlThick
// Add a Chart for the selected data.
oResizeRange = oWS.Range("E2:E6").Resize(5,iNumQtrs);
var oChart = oWS.Parent.Charts.Add();
oChart.ChartWizard(oResizeRange, -4100, null, 2); // -4100 = xl3dColumn
oChart.SeriesCollection(1).XValues = oWS.Range("A2", "A6");
for (iRet = 1; iRet <= iNumQtrs; iRet++) {
oChart.SeriesCollection(iRet).Name = "=\"Q" + iRet + "\"";
}
oChart.Location(2, oWS.Name); // 2 = xlLocationAsObject
// Move the chart so as not to cover your data.
oWS.Shapes("Chart 1").Top = oWS.Rows(10).Top;
oWS.Shapes("Chart 1").Left = oWS.Columns(2).Left;
}
</SCRIPT>
<P><INPUT id=button1 type=button value="创建Excel表格和柱状图" onclick="AutomateExcel"></P>
</BODY>
</HTML>
这里是在java类里面的方法
public void generateXls()
{
try
{
/** *//** **********创建工作簿************ */
WritableWorkbook workbook = Workbook.createWorkbook(new File("d:/test.xls"));
/** *//** **********创建工作表************ */
WritableSheet sheet = workbook.createSheet("工作表名称", 0);
/** *//** *********设置列宽**************** */
sheet.setColumnView(0, 15); // 第1列
sheet.setColumnView(1, 18); // 第2列
sheet.setColumnView(2, 13);
sheet.setColumnView(3, 13);
sheet.setColumnView(4, 15);
sheet.setColumnView(5, 15);
//设置行高
sheet.setRowView(0, 600, false);
sheet.setRowView(1, 400, false);
sheet.setRowView(7, 400, false);
//设置页边距
sheet.getSettings().setRightMargin(0.5);
//设置页脚
sheet.setFooter("", "", "测试页脚");
/** *//** ************设置单元格字体************** */
//字体
WritableFont NormalFont = new WritableFont(WritableFont.ARIAL, 10);
WritableFont BoldFont = new WritableFont(WritableFont.ARIAL, 14,
WritableFont.BOLD);
WritableFont tableFont = new WritableFont(WritableFont.ARIAL, 12,
WritableFont.NO_BOLD);
WritableFont baodanFont = new WritableFont(WritableFont.ARIAL, 10,
WritableFont.BOLD);
/** *//** ************以下设置几种格式的单元格************ */
// 用于标题
WritableCellFormat wcf_title = new WritableCellFormat(BoldFont);
wcf_title.setBorder(Border.NONE, BorderLineStyle.THIN); // 线条
wcf_title.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐
wcf_title.setAlignment(Alignment.CENTRE); // 水平对齐
wcf_title.setWrap(true); // 是否换行
// 用于表格标题
WritableCellFormat wcf_tabletitle = new WritableCellFormat(
tableFont);
wcf_tabletitle.setBorder(Border.NONE, BorderLineStyle.THIN); // 线条
wcf_tabletitle.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐
wcf_tabletitle.setAlignment(Alignment.CENTRE); // 水平对齐
wcf_tabletitle.setWrap(true); // 是否换行
// 用于正文左
WritableCellFormat wcf_left = new WritableCellFormat(NormalFont);
wcf_left.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
wcf_left.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐
wcf_left.setAlignment(Alignment.LEFT);
wcf_left.setWrap(true); // 是否换行
// 用于正文左
WritableCellFormat wcf_center = new WritableCellFormat(NormalFont);
wcf_center.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐
wcf_center.setAlignment(Alignment.CENTRE);
wcf_center.setWrap(true); // 是否换行
// 用于正文右
WritableCellFormat wcf_right = new WritableCellFormat(NormalFont);
wcf_right.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
wcf_right.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐
wcf_right.setAlignment(Alignment.RIGHT);
wcf_right.setWrap(false); // 是否换行
// 用于跨行
WritableCellFormat wcf_merge = new WritableCellFormat(NormalFont);
wcf_merge.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
wcf_merge.setVerticalAlignment(VerticalAlignment.TOP); // 垂直对齐
wcf_merge.setAlignment(Alignment.LEFT);
wcf_merge.setWrap(true); // 是否换行
WritableCellFormat wcf_table = new WritableCellFormat(NormalFont);
wcf_table.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
wcf_table.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐
wcf_table.setAlignment(Alignment.CENTRE);
wcf_table.setBackground(Colour.GRAY_25);
wcf_table.setWrap(true); // 是否换行
/** *//** ************单元格格式设置完成****************** */
//合并单元格,注意mergeCells(col0,row0,col1,row1) --列从0开始,col1为你要合并到第几列,行也一样
sheet.mergeCells(0, 0, 5, 0);
sheet.addCell(new Label(0, 0, "这里是大标题,自定义格式",
wcf_title));
sheet.mergeCells(0, 1, 1, 1);
sheet.mergeCells(2, 1, 5, 1);
sheet.addCell(new Label(0, 1, "", wcf_center));
sheet.addCell(new Label(2, 1, "姓名:" + "supercrsky",
wcf_center));
sheet.mergeCells(0, 2, 1, 2);
sheet.mergeCells(2, 2, 3, 2);
sheet.addCell(new Label(0, 2, "单位:", wcf_center));
sheet.addCell(new Label(2, 2, "ChinaLong", wcf_center));
sheet.addCell(new Label(4, 2, "薪水", wcf_center));
sheet.addCell(new Label(5, 2, "5000", wcf_center));
sheet.mergeCells(0, 3, 1, 3);
sheet.mergeCells(2, 3, 3, 3);
sheet.addCell(new Label(0, 3, "性别:", wcf_center));
sheet.addCell(new Label(2, 3, "男", wcf_center));
sheet.addCell(new Label(4, 3, "婚否:", wcf_center));
sheet.addCell(new Label(5, 3, "否", wcf_center));
sheet.mergeCells(0, 4, 1, 4);
sheet.mergeCells(2, 4, 3, 4);
sheet.addCell(new Label(0, 4, "是否在职:", wcf_center));
sheet.addCell(new Label(2, 4,"是",
wcf_center));
sheet.addCell(new Label(4, 4,"工作经验:", wcf_center));
sheet.addCell(new Label(5, 4, "4",wcf_center));
sheet.mergeCells(0, 5, 1, 5);
sheet.mergeCells(2, 5, 3, 5);
sheet.addCell(new Label(0, 5, "保险费:", wcf_center));
sheet.addCell(new Label(2, 5,"50",
wcf_center));
sheet.addCell(new Label(4, 5, "保险金额:", wcf_center));
sheet.addCell(new Label(5, 5, "50000",
wcf_center));
sheet.mergeCells(0, 6, 1, 6);
sheet.mergeCells(2, 6, 3, 6);
sheet.addCell(new Label(0, 6, "工作地点:", wcf_center));
sheet.addCell(new Label(2, 6, "北京", wcf_center));
sheet.addCell(new Label(4, 6, "开心度:", wcf_center));
sheet.addCell(new Label(5, 6, "一般", wcf_center));
// 另起一table
sheet.mergeCells(0, 7, 5, 7);
sheet.addCell(new Label(0, 7, "详细数据", wcf_tabletitle));
// table标题
sheet.addCell(new Label(0, 8, "序号", wcf_table));
sheet.addCell(new Label(1, 8, "姓名", wcf_table));
sheet.addCell(new Label(2, 8, "年龄", wcf_table));
sheet.addCell(new Label(3, 8, "性别", wcf_table));
sheet.addCell(new Label(4, 8, "婚否", wcf_table));
sheet.addCell(new Label(5, 8, "在职", wcf_table));
// table内容
//这里用你的dao
TestDAO dao = new TestDAO();
List list = dao.findBy(user.getUserId());
System.out.println("此保单拥有防疫码数量:" + list.size());
for (int i = 0; i < list.size(); i++)
{
//对应你的vo类
User data = (User) list.get(i);
sheet.addCell(new Label(0, 9 + i, String.valueOf(i + 1),
wcf_center));
sheet.addCell(new Label(1, 9 + i, data.getDlEPCode(),
wcf_center));
sheet
.addCell(new Label(2, 9 + i, data.getDlType(),
wcf_center));
sheet.addCell(new Label(3, 9 + i, String.valueOf(data
.getDlPigAge()), wcf_center));
sheet.addCell(new Label(4, 9 + i, "", wcf_center));
sheet.addCell(new Label(5, 9 + i, "", wcf_center));
}
/** *//** **********以上所写的内容都是写在缓存中的,下一句将缓存的内容写到文件中******** */
workbook.write();
/** *//** *********关闭文件************* */
workbook.close();
System.out.println("导出成功");
// 存放url地址
} catch (Exception e)
{
System.out.println("在输出到EXCEL的过程中出现错误,错误原因:" + e.toString());
}
}
外加一段读取流的代码: 呵呵非常有用哦!!
HttpServletResponse response = ServletActionContext.getResponse();
String fileChName="项目积分综合报表.xls";
response.reset();
response.setCharacterEncoding("ISO8859-1");
response.setHeader("Content-Disposition", "attachment; filename="
+ new String(fileChName.getBytes("GBK"), "ISO8859-1"));//fileChName为下载时用户看到的文件名
response.setHeader("Connection", "close");
response.setHeader("Content-Type", "application/octet-stream");
OutputStream out = response.getOutputStream();
WritableWorkbook wwb = Workbook.createWorkbook(out);
相关推荐
这个"JAVA JXL报表完美程序版"很可能是包含了一个完整的解决方案,用于创建具有表头、表内容和表统计列的Excel报表。下面将详细讨论JXL库的关键功能和如何在Java中使用它来实现这些特性。 1. **JXL库的基本概念**:...
String template=System.getProperty("user.dir")+"\\src\\template.xls";//模板文件 String book=System.getProperty("user.dir... Label a3=new Label(2,1,"拿大刀"); setFormat(a3); sheet.addCell(a3);
【标题】"jxl报表 代码应用"涉及的是在Java编程环境中使用jxl库来创建和处理Excel报表的实践案例,特别关注的是如何在SSH(Struts、Hibernate、Spring)框架下集成jxl进行数据展示。 【描述】"jxl实现的SSH架构的...
**JXL报表制作工具详解** JXL,全称为Java Excel API,是一款开源的Java库,专为处理Microsoft Excel文件而设计。它允许开发者在Java应用程序中读取、写入和修改Excel工作簿,无需依赖Excel应用程序本身。在企业级...
**JXL报表制作详解** JXL(Java Excel API)是一个流行的开源库,它允许Java开发者读写Microsoft Excel文件。在本篇文章中,我们将深入探讨如何使用JXL进行报表制作,并结合一个实例,来理解JXL封装类的用法,简化...
"jxl报表导入导出"是一个基于jxl库实现的功能,它允许开发者读取、写入和修改Excel文件,从而方便地进行数据操作。jxl库是一个开源的Java库,专门用于与Microsoft Excel文件进行交互,支持版本从Excel 97到2007。 1...
本篇将深入探讨如何使用JXL库在Java中打印Excel报表。 首先,要使用JXL库,你需要在项目中引入它的依赖。在本例中,`jxl.jar`是JXL库的核心文件,你需要将其添加到项目的类路径中。如果你使用的是Maven或者Gradle,...
总结来说,“Excel报表JXL类型 报表 从数据库解析 读取 排列 java”这个主题涵盖了使用Java的JXL库创建和处理Excel报表的核心步骤,包括从数据库获取数据,对数据进行排序,以及使用JXL的API在Excel文件中写入和格式...
本文将深入探讨`jxl`报表库以及如何结合`jsoup`进行报表的导入和解析。`jxl`是一个广泛使用的Java库,专门用于处理Excel文件,而`jsoup`则是一个强大的HTML解析器,可以用于解析XML格式的数据,包括高版本的Excel...
本文将详细探讨`jxl`库在导出Excel报表方面的应用,以及如何利用它来实现一个简单的报表生成过程。 首先,我们需要了解`jxl`库的基本结构。`jxl`库提供了多种类和接口,如`Workbook`、`Sheet`、`Cell`等,它们分别...
"jxl预览和打印报表"是一种利用开源Java库——jxl来实现的功能,它能够帮助开发者高效地处理Excel格式的数据,进行报表的生成、预览以及打印操作。下面将详细阐述这一技术的知识点。 首先,jxl库是一个强大的Java ...
2. **JExcelAPI(JXL)介绍**: JXL是另一个处理Excel文件的Java库,它提供了简单的API用于读写Excel文件。在描述中提到的"jxl-2.6.jar"是JXL的一个版本。虽然POI功能更强大,但JXL在某些场景下可能更易于使用,...
java报表JXL和POI打印设置
在IT行业中,生成Excel报表是常见的数据展示和分析任务,而JXL库为Java开发者提供了一个强大且便捷的解决方案。JXL,全称Java Excel API,是一个开源的Java库,专为读取、写入以及修改Excel文件设计。它允许程序员在...
总的来说,JXL库是Java开发者处理Excel文件的理想选择,无论你是需要读取Excel数据进行分析,还是构建数据导入导出系统,甚至是生成复杂的报表,JXL都能提供强大而全面的支持。通过熟练掌握JXL,你将能够更高效地与...
标题中的“jxl读取Excel报表”指的是利用jxl库来操作Excel文件,特别是从Excel文件中提取数据以进行分析或进一步处理。以下是对这个主题的详细讲解: 首先,`jxl.jar`是jxl库的主要组件,它支持读取和写入Microsoft...
2. **服务器端处理**:将JavaScript处理后的数据发送到服务器,然后使用Java和JXL库生成Excel文件。这里,服务器端的Java代码会接收这些数据,创建一个新的Excel工作簿,填充数据,并设置所需的格式和样式。 3. **...
### Java 报表 JXL 和 POI 打印设置详解 在Java开发中,经常需要处理Excel文件,尤其是在报表系统中。JXL 和 POI 是两个常用的库来操作Excel文件。其中,对于打印功能的支持是必不可少的一个环节。本文将详细介绍...
标题中的“jxls报表生成利器,比起jxl,poi牛逼得很”暗示了本文将讨论在Java环境下生成Excel报表的工具,重点在于jxls和Apache POI库的对比。jxls是一个基于Apache POI的扩展库,使得在处理Excel模板时更加方便高效...