十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
效果图
成都创新互联 - 服务器托管雅安,四川服务器租用,成都服务器租用,四川网通托管,绵阳服务器托管,德阳服务器托管,遂宁服务器托管,绵阳服务器托管,四川云主机,成都云主机,西南云主机,服务器托管雅安,西南服务器托管,四川/成都大带宽,成都机柜租用,四川老牌IDC服务商
参考代码
import java.awt.*;
import javax.swing.*;
public class GridFrame extends JFrame {
// 指定横向格子数量
static final int W_NUM = 3;
// 指定竖向格子数量
static final int H_NUM = 3;
// 指定每个格子的宽
static final int CELL_WIDTH = 50;
// 指定每个格子的高
static final int CELL_HEIGHT = 50;
CellPanel cp;
public GridFrame() {
cp = new CellPanel();
this.add(cp);
this.setBounds(350, 100, W_NUM * CELL_WIDTH+15 , H_NUM * CELL_HEIGHT
+ 40);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("网格窗口");
this.setVisible(true);
}
public static void main(String[] args) {
new GridFrame();
}
class CellPanel extends JPanel {
@Override
public void paint(Graphics g) {
super.paint(g);
for (int i = 0; i H_NUM; i++) {
for (int j = 0; j W_NUM; j++) {
g.setColor(new Color((int) (Math.random() * 256),
(int) (Math.random() * 256),
(int) (Math.random() * 256)));
g.fill3DRect(j * CELL_WIDTH, i * CELL_HEIGHT, CELL_WIDTH,
CELL_HEIGHT, true);
}
}
}
}
}
利用java操作Excel源东东-jxl.jar读取前单元格值 String cellValue = cells[k].getContents(); sb.append(cellValue+" "); } } sb.append("
"); } sb.append("
"); } } //关闭资源释放内存 wb.close(); return sb.toString(); } 二.写入Excel文件格式比文本内容加粗加某些颜色等参考jxlapi同推荐篇错文章:/developerworks/cn/java/l-javaExcel/?ca=j-t10java 代码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){ //创建写入工作表 //WorkbookcreateSheet两参数第工作表名称第二工作表工作薄位置 WritableSheet ws = wwb.createSheet("sheet1", 0); //面始添加单元格 for(int i=0;i0){ //每工作表进行循环 for(int i=0;iif(breakSheet) break; //前工作表行数 int rowNum = sheet[i].getRows(); boolean breakRow = false; for(int j=0;jif(breakRow) break; //前行所单元格 Cell[] cells = sheet[i].getRow(j); if(cells!=nullcells.length0){ boolean breakCell = false; //每单元格进行循环 for(int k=0;kif(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插入图片图标插入图片实现容易参看代码:java 代码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); } 代码注释已经清楚概用再解释我用程序验证:java 代码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格式都支持五.插入页眉页脚般页眉页脚都三部,左,,右三部,利用代码实现插入页眉页脚java 代码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); } 我用代码测试该:java 代码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(); } } 合并单元格://首先创建sheetWritableSheet sheetWrite=writeWorkbook.createSheet("sheet名称",0);// 单元格(column, row)单元格(column1, row1)进行合并sheetWrite.mergeCells(column, row, column1, row1);java 操作exceljxl加边框jxl合并单元格,单元格设置单元WritableCellFormat headerFormat = new WritableCellFormat(HEADER_FONT_STYLE);//水平居齐headerFormat.setAlignment(Alignment.CENTRE);//竖直向居齐headerFormat.setVerticalAlignment(VerticalAlignment.CENTRE);//建立标签参数依:列索引、行索引、内容、格式Label seqLabel = new Label(0, 0, "序号", headerFormat);//增加单元格sheet.addCell(seqLabel);//合并单元格参数依:列索引、行索引、列索引+需要合并列数、行索引+需要合并行数sheet.mergeCells(0, 0, 0, 1);//设置单元格宽度字符单位sheet.setColumnView(0, "序号".length() + 10);Label basicInfoLabel = new Label(1, 0, "基本信息",headerFormat);sheet.addCell(basicInfoLabel);sheet.mergeCells(1, 0, 3, 0);//合并垮3行sheet.mergeCells(1, 1, 1, 3);//合并垮3列label = new Label(0, 1, "苹);sheet.mergeCells(0,1, 0, 3- 1);//合并垮3列WritableCellFormat cellFormat = new WritableCellFormat();cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);// 垂直居label.setCellFormat(cellFormat);sheet.addCell(label);注:再使cellFormat需要重新NEW WritableCellFormatjava怎么操作excel合并单元格
import java.util.Scanner;
public class test
{
int[] number;
int[][] op;
public void operation(int p,int start,int end)
{
if(p==1){number[start-1]=end;}
else if(p==2)
{ int sum=0;
for(int i=start-1;iend;i++)sum+=number[i];
System.out.println(sum);
}
else if(p==3)
{int max=Integer.MIN_VALUE;
for(int i=start-1;iend;i++)if(number[i]max)max=number[i];
System.out.println(max);
}
else
{
System.out.println("非法操作");
}
}
public static void main(String[] args)
{
int i=0,j=0;
test t=new test();
int n=0,m=0;
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
m=sc.nextInt();
t.number=new int[n];
for(i=0;in;i++)t.number[i]=sc.nextInt();
t.op=new int[m][];
for(i=0;im;i++)
{
t.op[i]=new int[3];
for(j=0;j3;j++)
t.op[i][j]=sc.nextInt();
}
for(i=0;im;i++)
{
t.operation(t.op[i][0],t.op[i][1],t.op[i][2]);
}
}
}
java表格就是java swing。
//创建表头
String[] columnNames = { "First Name", "Last Name", "Sport",
"# of Years", "Vegetarian" };
//创建显示数据
Object[][] data = {
{ "Kathy", "Smith", "Snowboarding", new Integer(5),
new Boolean(false) },
{ "John", "Doe", "Rowing", new Integer(3), new Boolean(true) },
{ "Sue", "Black", "Knitting", new Integer(2),
new Boolean(false) },
{ "Jane", "White", "Speed reading", new Integer(20),
new Boolean(true) },
{ "Joe", "Brown", "Pool", new Integer(10), new Boolean(false) } };
/*
* JTable还提供了一个重载的构造方法,传入两个Vector
* JTable(Vector rowData, Vector columnNames)
*
*/
final JTable table = new JTable(data, columnNames);
table.setBackground(Color.YELLOW);
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
* @param inputFile 输入模板文件路径
* @param outputFile 输入文件存放于服务器路径
* @param dataList 待导出数据
* @throws Exception
* @roseuid:
*/
public void exportExcelFile(String inputFile, String outputFile, List dataList) throws Exception
{
//用模板文件构造poi
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(inputFile));
//创建模板工作表
HSSFWorkbook templatewb = new HSSFWorkbook(fs);
//直接取模板第一个sheet对象
HSSFSheet templateSheet = templatewb.getSheetAt(1);
//得到模板的第一个sheet的第一行对象 为了得到模板样式
HSSFRow templateRow = templateSheet.getRow(0);
//HSSFSheet timplateSheet = templatewb.getSheetAt(1);
//取得Excel文件的总列数
int columns = templateSheet.getRow((short) 0).getPhysicalNumberOfCells();
Debug.println("columns is : " + columns);
//创建样式数组
HSSFCellStyle styleArray[] = new HSSFCellStyle[columns];
//一次性创建所有列的样式放在数组里
for (int s = 0; s columns; s++)
{
//得到数组实例
styleArray[s] = templatewb.createCellStyle();
}
//循环对每一个单元格进行赋值
//定位行
for (int rowId = 1; rowId dataList.size(); rowId++)
{
//依次取第rowId行数据 每一个数据是valueList
List valueList = (List) dataList.get(rowId - 1);
//定位列
for (int columnId = 0; columnId columns; columnId++)
{
//依次取出对应与colunmId列的值
//每一个单元格的值
String dataValue = (String) valueList.get(columnId);
//取出colunmId列的的style
//模板每一列的样式
HSSFCellStyle style = styleArray[columnId];
//取模板第colunmId列的单元格对象
//模板单元格对象
HSSFCell templateCell = templateRow.getCell((short) columnId);
//创建一个新的rowId行 行对象
//新建的行对象
HSSFRow hssfRow = templateSheet.createRow(rowId);
//创建新的rowId行 columnId列 单元格对象
//新建的单元格对象
HSSFCell cell = hssfRow.createCell((short) columnId);
//如果对应的模板单元格 样式为非锁定
if (templateCell.getCellStyle().getLocked() == false)
{
//设置此列style为非锁定
style.setLocked(false);
//设置到新的单元格上
cell.setCellStyle(style);
}
//否则样式为锁定
else
{
//设置此列style为锁定
style.setLocked(true);
//设置到新单元格上
cell.setCellStyle(style);
}
//设置编码
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
//Debug.println("dataValue : " + dataValue);
//设置值 统一为String
cell.setCellValue(dataValue);
}
}
//设置输入流
FileOutputStream fOut = new FileOutputStream(outputFile);
//将模板的内容写到输出文件上
templatewb.write(fOut);
fOut.flush();
//操作结束,关闭文件
fOut.close();
}