多表头导出Excel

procedure   TForm1.Button1Click(Sender:   TObject);   
          var   
                  AppExcel:variant;   
  begin   
          AppExcel:
= CreateOleObject( ' Excel.Application');   
          AppExcel.visible: = true ;   
          AppExcel.WorkBooks.add();   
          AppExcel.WorkBooks[
1 ].WorkSheets[ 1 ].Range[ ' A1:C2'].select;   
          AppExcel.Selection.Merge;   
  
end ;   
 
  public    
          ExcelApp:   Variant;   
          dbgrideh2:Tdbgrideh;   
          query2:Tquery;   
    
function   tocell(i,   j:   integer):   string;                               //把行列转换为EXCEL的单元格的格式   
    
function   power26(x:   Integer):   Integer;                                 //26的x次方函数   
    
function   inttoz26(value:   Integer):   String;                         //整数转换为excel形式的26进制函数   
    
function   z26toint(value:   string):   Integer;                         //整数转换为excel形式的26进制的逆函数   
    
function   excel_open(filename,appCaption:string):integer;   
    
function   excel_merge(title_row,title_col,all_rowcount:integer):integer;   
    procedure   outtoexcel(filename,appcaption:
string;dbgrideh:Tdbgrideh;cs_jqdh:integer;cs_s1:string;cs_s2:string;cs_i1:integer;cs_i2:integer);   
    
        {   
Public   declarations   }   
    
end;   
    
  var   
    Form1:   TForm1;   
      
  implementation   
    
  {$R   
*.DFM}   
    
    
  
function   TForm1.z26toint(value:   string):   Integer;   
  var   
    i:   
Integer;   
  begin   
    result   :
=   0;   
    
for   i   :=   1   to   length(value)   do   
    begin   
        result   :
=   result   +   (ord(upcase(value[i]))   -   64)*power26(length(value)-i);   
    
end;   
  
end;   
    
  
function   TForm1.inttoz26(value:   Integer):   String;   
  var   
    
left,d:   Integer;   
    c:   
char;   
  begin   
    
left   :=   value;   
    result   :
=   '';   
    while   left>0   do   
    begin   
        d   :
=   left   mod   26;   
        
left   :=   left   div   26;   
        
if   d   =   0   then   
        begin   
            c   :
=   'Z';   
            dec(left);   
        
end   
        
else   
            c   :
=   chr(d+64);   
        result   :
=   c   +   result;   
    
end;   
  
end;   
    
  
function   TForm1.power26(x:   Integer):   Integer;   
  var   
    m:   
Integer;   
  begin   
    result   :
=   1;   
    
for   m   :=   1   to   x   do   
        result   :
=   26*result;   
  
end;   
    
  
function   TForm1.tocell(i,   j:   integer):   string;   
  begin   
    result   :
=inttoz26(j)+inttostr(i);   
  
end;   
    
  
function   TForm1.excel_open(filename,appcaption:string):integer;   
  begin   
          
try   
          result:
=-1;   
          ExcelApp   :
=   CreateOleObject(   'Excel.Application'   );   
          ExcelApp.Visible   :=   True;   
          ExcelApp.Caption   :
=   appcaption;   
          ExcelApp.WorkBooks.Open(   filename   );   
          result:
=1;   
          except   
                application.MessageBox(
'没有安装EXCEL!!','提示',64);   
          end;   
  
end;   
    
  
function   TForm1.excel_merge(title_row,title_col,all_rowcount:integer):integer;   
  var   
      str_title,str_all,str_data,excel_start,excel_end:
string;   
      i,j,k,l,jl_col,jl_row   :   
integer;   
      ls_row,ls_col,ls_rowcount:
integer;   
      arr   :array   
of   array   of   integer;   
      arr_str:array   
of   string;   
  begin   
          ls_row:
=title_row;   
          ls_col:
=title_col;   
          ls_rowcount:
=all_rowcount;   
          setlength(arr,ls_row
+1);         //初始化   
          
for   i:=0   to   ls_row   do   
          begin   
                setlength(arr[i],ls_col
+1);   
          
end;   
          
for   i:=0   to   length(arr)-1   do   
          begin   
                  
for   j:=0   to   length(arr[i])-1   do   
                        arr[i][j]:
=0;   
          
end;   
          setlength(arr_str,
0);   
          
for   i:=1   to   ls_row   do   
          begin   
                    
for   j:=1   to   ls_col   do   
                    begin   
                            
if   trim(ExcelApp.Cells[i,j].Value)<>''   then   
                            begin   
                                  excel_start:
=tocell(i,j);   
                                  arr[i][j]:
=1;     //表示已经使用   
                                  jl_col:
=j;   
                                  
for   l:=j+1   to   ls_col   do   
                                  begin   
                                        
if   (trim(ExcelApp.Cells[i,l].Value)='')   and   (arr[i][l]<>1)   then   
                                        begin   
                                                jl_col:
=l;   
                                                arr[i][l]:
=1;   
                                        
end   else   
                                              break;   
                                  
end;   
                                  jl_row:
=i;   
                                  
for   l:=i+1   to   ls_row   do   
                                  begin   
                                        
if   (trim(ExcelApp.Cells[l,j].Value)='')   and   (arr[l][j]<>1)   then   
                                        begin   
                                                    jl_row:
=l;   
                                                    arr[l][j]:
=1;   
                                        
end   else   
                                        begin   
                                                break;   
                                        
end;   
                                  
end;   
                                  excel_end:
=tocell(jl_row,jl_col);   
                                  
if   excel_start<>excel_end   then   
                                  begin   
                                  str_title:
=excel_start+':'+excel_end;   
                                  setlength(arr_str,length(arr_str)+1)   ;   
                                  arr_str[length(arr_str)
-1]:=str_title;   
                                  
end;   
                            
end;   
                    
end;   
          
end;   
    
          str_title:
=   'A1:'+tocell(ls_row,ls_col);   
          for   i:=0   to   length(arr_str)-1   do   
          begin   
              ExcelApp.Range[arr_str[i]].Select;   
              ExcelApp.Selection.Merge;   
          
end;   
          ExcelApp.ActiveSheet.range[str_title].Font.Bold       :
=   True;   
          ExcelApp.ActiveSheet.range[str_title].HorizontalAlignment:
=3;     //居中   
          ExcelApp.ActiveSheet.range[str_title].VerticalAlignment:
=2;         //居中   
          ExcelApp.ActiveSheet.range[str_title].Borders.LineStyle   :
=   1;     //加边框   
          ExcelApp.ActiveSheet.Range[str_title].Borders[
1].Weight   :=   3;   
          ExcelApp.ActiveSheet.Range[str_title].Borders[
2].Weight   :=   3;   
          ExcelApp.ActiveSheet.Range[str_title].Borders[
3].Weight   :=   3;   
          ExcelApp.ActiveSheet.Range[str_title].Borders[
4].Weight   :=   3;   
          ExcelApp.ActiveSheet.range[str_title].Interior.ColorIndex   :
=19   ;   //鹅黄色   
          str_all:
='A1:'+tocell(ls_rowcount+ls_row,ls_col);   
          str_data:=tocell(ls_row+1,1)+':'+tocell(ls_rowcount+ls_row,ls_col);   
          ExcelApp.ActiveSheet.range[str_data].Borders.LineStyle   :=   1;   
          ExcelApp.ActiveSheet.range[str_all].Columns.AutoFit;   
    
          
//ExcelApp.ActiveSheet.Range[   'A1:D3'   ].Borders[5].Weight   :=   3;   //交叉   
          //ExcelApp.ActiveSheet.Range[   'A1:D3'   ].Borders[6].Weight   :=   3;     //交叉   
          //ExcelApp.ActiveWorkBook.Saved   :=   true;   //放弃存盘   
          
//ExcelApp.Cells[4,1].Value:='反复发';   
          if   not   ExcelApp.ActiveWorkBook.Saved   then       //保存   
                ExcelApp.ActiveWorkBook.Save;   
          ExcelApp:
=Unassigned;   
            
//   ExcelApp.quit;   
  
end;   
  procedure   TForm1.outtoexcel(filename,appcaption:
string;dbgrideh:Tdbgrideh;cs_jqdh:integer;cs_s1:string;cs_s2:string;cs_i1:integer;cs_i2:integer);   
  var   
      dbgridehexp   :TDBGridEhExportAsXLS;   
      i   :   
integer;   
  begin   
          dbgridehexp   :
=   TDBGridEhExportAsXLS.Create();   
          dbgridehexp.DBGridEh:
=dbgrideh2;   
          dbgridehexp.ExportToFile(filename,
TRUE);         //直接导出文件   
          
if   excel_open(filename,appcaption)=-1   then         //打开excel   
                
exit;   
          excel_merge(dbgridehexp.MutiTitle_RowCount,dbgridehexp.MutiTitle_ColCount,dbgridehexp.DBGridEh.DataSource.DataSet.Recordcount);   
                    
//写数据格式   
  
end;         
    
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现Java导出Excel表头或多表头,可以使用Apache POI库。下面是一个简单的示例代码,可以导出带有单表头和多表头Excel文件: ```java public static void exportExcel(List<List<String>> data, List<String> headers, String filePath) throws IOException { Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet(); // 创建单表头 Row headerRow = sheet.createRow(0); for (int i = 0; i < headers.size(); i++) { Cell cell = headerRow.createCell(i); cell.setCellValue(headers.get(i)); } // 创建多表头 if (data.size() > 0) { Row subHeaderRow = sheet.createRow(1); List<String> subHeaders = data.get(0); for (int i = 0; i < subHeaders.size(); i++) { Cell cell = subHeaderRow.createCell(i); cell.setCellValue(subHeaders.get(i)); } } // 填充数据 int rowIndex = data.size() > 0 ? 2 : 1; for (List<String> rowData : data) { Row row = sheet.createRow(rowIndex++); for (int i = 0; i < rowData.size(); i++) { Cell cell = row.createCell(i); cell.setCellValue(rowData.get(i)); } } // 输出文件 try (FileOutputStream outputStream = new FileOutputStream(filePath)) { workbook.write(outputStream); } } ``` 这个方法接受一个包含数据的二维列表、表头列表和输出文件路径的参数。它首先创建一个新的工作簿和一个新的工作表,然后填充单表头和多表头行,最后填充数据并将结果写入文件。注意,在创建多表头行时,我们需要检查数据列表是否为空,以避免出现数组越界异常。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值